files/en-us/web/api/htmltablerowelement/insertcell/index.md
{{APIRef("HTML DOM")}}
The insertCell() method of the {{domxref("HTMLTableRowElement")}} interface inserts a new
cell ({{HtmlElement("td")}}) into a table row ({{HtmlElement("tr")}}) and returns a
reference to the cell.
[!NOTE]
insertCell()inserts the cell directly into the row. The cell does not need to be appended separately with {{domxref("Node.appendChild()")}} as would be the case if {{domxref("Document.createElement()")}} had been used to create the new<td>element.You can not use
insertCell()to create a new<th>element though.
insertCell()
insertCell(index)
index {{optional_inline}}
index is -1 or equal to the number of cells, the cell is appended as the last cell in the row. If index is omitted it defaults to -1.An {{domxref("HTMLTableCellElement")}} that references the new cell.
IndexSizeError {{domxref("DOMException")}}
index is greater than the number of cells.This example uses HTMLTableRowElement.insertCell() to append a new cell to a
row.
<table>
<thead>
<tr>
<th>C1</th>
<th>C2</th>
<th>C3</th>
<th>C4</th>
<th>C5</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</tbody>
</table>
<button id="add">Add a cell</button>
<button id="remove">Remove last cell</button>
<div>This first row has <output>2</output> cell(s).</div>
table {
border-collapse: collapse;
}
th,
td,
table {
border: 1px solid black;
}
button {
margin: 1em 1em 1em 0;
}
// Obtain relevant interface elements
const bodySection = document.querySelectorAll("tbody")[0];
const row = bodySection.rows[0]; // Select the first row of the body section
const cells = row.cells; // The collection is live, therefore always up-to-date
const cellNumberDisplay = document.querySelectorAll("output")[0];
const addButton = document.getElementById("add");
const removeButton = document.getElementById("remove");
function updateCellNumber() {
cellNumberDisplay.textContent = cells.length;
}
addButton.addEventListener("click", () => {
// Add a new cell at the end of the first row
const newCell = row.insertCell();
newCell.textContent = `Cell ${cells.length}`;
// Update the row counter
updateCellNumber();
});
removeButton.addEventListener("click", () => {
// Delete the row from the body
row.deleteCell(-1);
// Update the row counter
updateCellNumber();
});
{{EmbedLiveSample("Examples", "100%", 175)}}
{{Specifications}}
{{Compat}}