DOM Methods

HTMLTableRowElement.insertCell()

Creates and inserts a td cell into a row. The optional index selects insertion position; -1 appends. This method always creates a data cell, not a th. Create a th explicitly when a header is needed. Example result: td / 2.

Formal Syntax & Structure

html syntaxhtml
element.insertCell(index)

Parameters & Attributes

NameTypeDescription
indexnumberOptional cell index, -1 appends.

Return Value

HTMLTableCellElement

Creates and inserts a td cell into a row.

Working Example

Example Snippethtml
<table>  <tr id="demo">    <td>A</td>  </tr></table><p>  Result:  <output id="result"></output></p><script>  const e = document.getElementById('demo');  const result = value => { document.getElementById("result").textContent = String(value); };  const cell=e.insertCell(-1);cell.textContent='B';result(cell.localName+' / '+e.cells.length);</script>
HTML Reference