I would like to create table inside a div element
On my .html file, I have this
<div id='div1'> </div>
On my js file, I want to place new table with rows and with data
How can I implement this?
Assuming you have the HTML for the table, you can simply create a jQuery object out of it and append it to the DIV. If you have the data, you'll need to iterate through it and create the cells/rows from the data and add them independently.
$('<table><tr><td>.....</td></tr></table>').appendTo( '#div1' );
or
var data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [7, 8, 9 ] ];
var html = '<table><thead><tr>...</tr></thead><tbody>';
for (var i = 0, len = data.length; i < len; ++i) {
html += '<tr>';
for (var j = 0, rowLen = data[i].length; j < rowLen; ++j ) {
html += '<td>' + data[i][j] + '</td>';
}
html += "</tr>";
}
html += '</tbody><tfoot><tr>....</tr></tfoot></table>';
$(html).appendTo('#div1');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With