In this section, we will learn that the insertCell() method is and how to use it in JavaScript.
JavaScript Add Cell to Table: insertCell() Method
The JavaScript insertCell() method is used to insert a new cell in the row of a table.
Note that this method should be invoked on a reference to the <tr> element of a table.
Note: you can use the deleteCell() method to remove a cell from a table row.
insertCell() Method Syntax:
rowElement.insertCell(index)
insertCell() Method Parameter
The method takes one argument, and that is the index number where we want to insert the new cell.
The value 0 means insert the cell as the first cell of the target <tr>.
The value -1 means insert the cell as the last cell of the target <tr>.
This value is optional and, if ignored, the new cell will be inserted as the last cell of the target <tr> element.
insertCell() Method Return Value
The method returns a reference to the newly created cell.
Example: add cell to table JavaScript
See the Pen add cell to table JavaScript by Omid Dehghan (@odchan1) on CodePen.
Note: please check the insertRow() section if you’re not familiar with this method.