Skip to content

Instantly share code, notes, and snippets.

@mateuszkocz
Created September 15, 2013 16:19
Show Gist options
  • Save mateuszkocz/6572139 to your computer and use it in GitHub Desktop.
Save mateuszkocz/6572139 to your computer and use it in GitHub Desktop.
Create tables dynamically.
var table = document.createElement('table'),
tbody = document.createElement('tbody'),
i, rowcount;
table.appendChild(tbody);
for (i = 0; i <= 9; i++) {
rowcount = i + 1;
tbody.insertRow(i);
tbody.rows[i].insertCell(0);
tbody.rows[i].insertCell(1);
tbody.rows[i].insertCell(2);
tbody.rows[i].cells[0].appendChild(
document.createTextNode('Row ' + rowcount + ', Cell 1')
);
tbody.rows[i].cells[1].appendChild(
document.createTextNode('Row 1, Cell 2')
);
tbody.rows[i].cells[2].appendChild(
document.createTextNode('Row 1, Cell 3')
);
}
table.createCaption();
table.caption.appendChild(
document.createTextNode('A DOM-generated Table')
);
document.body.appendChild(table);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment