HTMLTableRowElement.insertCell()

The HTMLTableRowElement.insertCell() method inserts a new cell into a table row and returns a reference to the cell.

Syntax

var cell = HTMLTableRowElement.insertCell(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 cells, the cell is appended as the last cell in the row. If index is greater than the number of cells, 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>
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.

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'HTMLTableRowElement.insertCell()' in that specification.
Living Standard  
Document Object Model (DOM) Level 2 HTML Specification
The definition of 'HTMLTableRowElement.insertCell()' in that specification.
Obsolete Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Index parameter is optionalChrome Full support YesEdge Full support YesFirefox Full support 20IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 20Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Support for -1 as an index argumentChrome Full support YesEdge Full support YesFirefox Full support 20IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 20Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

See also

Document Tags and Contributors

Last updated by: connorshea,