Reputation: 11557
How can I dynamically generate an HTML <table>
with a variable number of rows?
The number of rows will depend on the number of properties that exist within a Javascript object.
function showTable(trnum) //number of table rows passed in
{
// how?
// $("#elem").foo // #elem - element container for table
}
Upvotes: 0
Views: 3634
Reputation: 682
You could give this a try:
$.createTable = function(trnum)
{
var reps = new Array(trnum);
var table = $('<table></table>');
$.each(reps,function(){
var td = $('<tr><td> Stuff here </td></tr>');
table.append(td)
});
$('#elem').append(table);
}
Calling function:
$.createTable(6);
Upvotes: 0
Reputation: 3873
function showTable(trnum) {
var tableCode = "<table>";
for (var i=0; i<trnum; i++) {
tableCode += "<tr>" + "stuff inside each tr ?" + "</tr>";
}
tableCode += "</table>";
$("#elem").append(tableCode);
}
Upvotes: 2