HTMLTableRowElement.insertCell()

  • Revision slug: DOM/tableRow.insertCell
  • Revision title: tableRow.insertCell
  • Revision id: 383341
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef() }}

Summary

Inserts a new cell into a table row and returns a reference to the cell.

Syntax

var cell = HTMLTableRowElement.insertCell(optional index = -1);
  • HTMLTableRowElement is a reference to an HTML table row element.
  • index is the cell index of the new cell.
  • cell is assigned a reference to the new cell.
    If index is -1 or equal to the number of cell, the cell is appended as the last cell in the row. If index is greater than the number of rows, an IndexSizeError exception will result. If index is omitted it defaults to -1.

Example

<table>
  <tr id="row0">
    <td>Original cell</td>
  </tr>
</table>

<script type="text/javascript">

  function addCell(tableRowID)
  {

    // Get a reference to the tableRow
    var rowRef = document.getElementById(tableRowID);

    // Insert a cell in the row at cell index 0
    var newCell   = rowRef.insertCell(0);

    // Append a text node to the cell
    var newText  = document.createTextNode('New cell')
    newCell.appendChild(newText);
  }

// Call addCell() with the ID of a table row
addCell('row0');

</script>

To be valid in an HTML document, a TR must have at least one TD element.

Note that insertCell inserts the cell directly into the table and returns a reference to the new cell. The cell does not need to be appended separately as would be the case if document.createElement() had been used to create the new TD element.

Browser compatibility

Gecko-specific notes

  • Starting with Gecko 20.0 (Firefox 20.0 / Thunderbird 20.0 / SeaMonkey 2.17) the index argument has been made optional and defaults to -1 as per HTML specification.

Specification

Revision Source

<p>{{ DomRef() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Inserts a new cell into a table row and returns a reference to the cell.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
var <i>cell</i> = <i>HTMLTableRowElement</i>.insertCell(optional <i>index = -1</i>);
</pre>
<ul>
  <li><code>HTMLTableRowElement</code> is a reference to an HTML table row element.</li>
  <li><code>index</code> is the cell index of the new cell.</li>
  <li><code>cell</code> is assigned a reference to the new cell.<br />
    If <code>index</code> is -1 or equal to the number of cell, the cell is appended as the last cell in the row. If <code>index</code> is greater than the number of rows, an IndexSizeError exception will result. If index is omitted it defaults to -1.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre>
&lt;table&gt;
  &lt;tr id="row0"&gt;
    &lt;td&gt;Original cell&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;script type="text/javascript"&gt;

  function addCell(tableRowID)
  {

    // Get a reference to the tableRow
    var rowRef = document.getElementById(tableRowID);

    // Insert a cell in the row at cell index 0
    var newCell   = rowRef.insertCell(0);

    // Append a text node to the cell
    var newText  = document.createTextNode('New cell')
    newCell.appendChild(newText);
  }

// Call addCell() with the ID of a table row
addCell('row0');

&lt;/script&gt;
</pre>
<p>To be valid in an HTML document, a TR must have at least one TD element.</p>
<p>Note that <code>insertCell</code> inserts the cell directly into the table and returns a reference to the new cell. The cell does not need to be appended separately as would be the case if <code><a href="en/DOM/document.createElement">document.createElement()</a></code> had been used to create the new TD element.</p>
<h2 name="Specification">Browser compatibility</h2>
<h3 id="Specification" name="Specification">Gecko-specific notes</h3>
<ul>
  <li>Starting with Gecko 20.0 (Firefox 20.0 / Thunderbird 20.0 / SeaMonkey 2.17) the index argument has been made optional and defaults to -1 as per HTML specification.</li>
</ul>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-68927016">DOM Level 2 HTML: insertCell</a></li>
  <li><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/tabular-data.html#dom-tr-insertcell">HTML Living Standard HTMLTableRowElement.insertCell</a></li>
</ul>
Revert to this revision