DrStrangeLove
DrStrangeLove

Reputation: 11557

Dynamic table generation in jQuery

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

Answers (3)

Limpan
Limpan

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

Sylvain
Sylvain

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

MUS
MUS

Reputation: 1450

Follow the link. Generating HTML Tables with jQuery. For demo go here

Upvotes: 0

Related Questions