Search w3schools.com:

SHARE THIS PAGE

tr insertCell() Method

tr Object Reference tr Object

Definition and Usage

The insertCell() method inserts a cell into the current row.

Syntax

trObject.insertCell(index)

Value Description
index An integer (starts at 0) that specifies the position of the new cell in the current row. The value of -1 can also be used; which results in that the new cell will be inserted at the last position.

If this parameter is omitted, insertCell() inserts the new cell at the last position in IE and at the first position in Chrome and Safari.

This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The insertCell() method is supported in all major browsers.


Example

Example

Insert new cell(s) in the first table row:

<html>
<head>
<script>
function displayResult()
{
var firstRow=document.getElementById("myTable").rows[0];
var x=firstRow.insertCell(-1);
x.innerHTML="New cell"
}
</script>
</head>
<body>

<table id="myTable" border="1">
  <tr>
    <td>First cell</td>
    <td>Second cell</td>
    <td>Third cell</td>
  </tr>
</table>
<br>
<button type="button" onclick="displayResult()">Insert cell</button>

</body>
</html>

Try it yourself »


Examples

More Examples

Add a new row to a table - then add cells and content


tr Object Reference tr Object

W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The HTML5 Certificate documents your knowledge of advanced HTML5.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]