Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67411

How to show two html tables using different table id's?

$
0
0

I want to display two html tables using different table id's. I am able to display one table but I am not able to display the second table and not figure it out where I am making error.

Code is given below:

var $container = $("#container");
 var $row = $("#container table tbody tr");

 // Loop through items in JSON data..
   var $button = $("<button>" + 'xyz' + "</button>");
   $container.prepend($button);
   var table = $("<table1>");
   table.append($("<tr><th>column1</th><th>column2</th></tr>"));
	 // Button click handler..
   $button.on("click", function(e) {
   e.preventDefault();
   for( var i=0; i<2; i++) {
     // Replace row HTML..
     //parent row
     var row=$('<tr ><td>' + 'data' + '</td>' +  + '<td>' + "" + '</td></tr>');
     table.append(row);
     for(var j =0; j<2; j++) {
     var row=$('<tr><td>' + "" + '</td></tr>');
     $('<td>data</td>')
          .on('click', function() { 
          //I want to display table2 when clicked on col2 data
          var table = $("<table2>");
          table.append($("<tr><th>col1</th><th>col2</th></tr   >"));
          var row=$('<tr><td>' + 'data' + '</td>' +  + '<td>' + "" + '</td></tr>');
          table.append(row);
          })
          .appendTo(row);
          table.append(row);
          $("#table2").html(table);
     }
 }    
     $("#table1").html(table);


     // Show table if it's not already visible..

   });
#table2 {
   margin-top: 20px;
   border-collapse: collapse;
   border: 1px solid silver;
   width: 500px;
 }


#table1 {
   margin-top: 20px;
   border-collapse: collapse;
   border: 1px solid silver;
   width: 500px;
 }

#table1 th {
   border: 1px solid black;
   text-align: left;
 }
#table1 td {
  border: 1px solid black;
  text-align: left;
  }
#table1 tr {
  background-color: #f2f2f2;
  }
  
 button {
   margin-left: 15px;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="container"><div id="table1"></div></div><div id="table2"></div>

I want to show second table when i click on values of column2. Kindly guide me how to fix this issue. Here is the full code- https://jsfiddle.net/gaurav10022/styjk9vr/48/


Viewing all articles
Browse latest Browse all 67411

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>