Mozilla.com

  1. MDC
  2. Main Page
  3. DOM
  4. table.insertRow

« Gecko DOM Reference

Summary

insertRow inserts a new row in the table.

Syntax

var row = HTMLTableElement.insertRow(index);
  • HTMLTableElement is a reference to a HTML table element.
  • index is the row index of the new row.
  • row is assigned a reference to the new row.
    If index is -1 or equal to the number of rows, the row is appended as the last row. If index is omitted or greater than the number of rows, an error will result.

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.

Page last modified 02:24, 3 Feb 2007 by RobG?

Files (0)