DOM:table.insertRow
From MDC
Contents |
[edit] Summary
insertRow inserts a new row in the table.
[edit] Syntax
var row = HTMLTableElement.insertRow(index);
-
HTMLTableElementis a reference to a HTML table element. -
indexis the row index of the new row. -
rowis assigned a reference to the new row.
Ifindexis -1 or equal to the number of rows, the row is appended as the last row. Ifindexis omitted or greater than the number of rows, an error will result.
[edit] Example
<table id="TableA">
<tr>
<td>Old top row</td>
</tr>
</table>
<script type="text/javascript">
function addRow(tableID)
{
// Get a reference to the table
var tableRef = document.getElementById(tableID);
// Insert a row in the table at row index 0
var newRow = tableRef.insertRow(0);
// Insert a cell in the row at index 0
var newCell = newRow.insertCell(0);
// Append a text node to the cell
var newText = document.createTextNode('New top row')
newCell.appendChild(newText);
}
// Call addRow() with the ID of a table
addRow('TableA');
</script>
To be valid in an HTML document, a TR must have at least one TD element.
Note that insertRow inserts the row directly into the table and returns a reference to the new row. The row does not need to be appended separately as would be the case if document.createElement() had been used to create the new TR element.