DOM Methods

HTMLTableElement.insertRow()

Creates and inserts a row at the specified table-row index. Omit the index or use -1 to append. Out-of-range indices can throw IndexSizeError. Use semantic header cells and meaningful table sections for accessible data. Example result: 2.

Formal Syntax & Structure

html syntaxhtml
element.insertRow(index)

Parameters & Attributes

NameTypeDescription
indexnumberOptional row index, -1 appends.

Return Value

HTMLTableRowElement

Creates and inserts a row at the specified table-row index.

Working Example

Example Snippethtml
<table id="demo">  <tbody>    <tr>      <td>1</td>    </tr>  </tbody></table><p>  Result:  <output id="result"></output></p><script>  const e = document.getElementById('demo');  const result = value => { document.getElementById("result").textContent = String(value); };  const row=e.insertRow(-1); row.insertCell().textContent='2'; result(e.rows.length);</script>
HTML Reference