Skip to content Skip to sidebar Skip to footer

Dynamically Creating A Table In Javascript

I have got a working table that when the button is pushed, a table is dynamically created by looping through an array and displaying that data in each cell. However the problem ari

Solution 1:

You can use a loop to avoid duplicating code. For example:

var columns = ["Name", "Age", "Degree"];

var thd = document.createElement("thead");
tab.appendChild(thd);

var tr = document.createElement("tr"); 
thd.appendChild(tr);

for (var i = 0; i < columns.length; i++) {
    var th = document.createElement("th");
    th.appendChild(document.createTextNode(columns[i]));
    tr.appendChild(th);    
}

Demo: http://jsfiddle.net/ahEkH/6/


Post a Comment for "Dynamically Creating A Table In Javascript"