X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

Table cells Collection

Table Object Reference Table Object

Example

Show the number of cells in the first row:

var x = document.getElementById("myTable").rows[0].cells.length;
document.getElementById("demo").innerHTML=x;

The result of x will be:

2

Try it yourself »


Definition and Usage

The cells collection returns a collection of all <td> or <th> elements in a table.

Note: The elements in the collection are sorted as they appear in the source code.

Tip: Use the rows collection to return a collection of all <tr> elements in a table.

Tip: Use the insertRow() method to create a new row (<tr>).

Tip: Use the deleteRow() method to remove a row.

Tip: Use the insertCell() method to create a new cell (<td>).

Tip: Use the deleteCell() method to delete a cell.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The cells collection is supported in all major browsers.


Syntax

tableObject.cells

Properties

Property Description
length Returns the number of <td> or <th> elements in the collection

Methods

Method Description
[name_or_index] A string or integer that specifies the element to retrieve (index starts at 0)
item(name_or_index) Returns the element from the collection with the specified index/name/id
namedItem(name_or_id) Returns the element from the collection with the specified name (name or id attribute)


More Examples

Example

[name_or_index]

Alert the innerHTML of the first cell in the table's first row:

alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);

Try it yourself »

Example

item(name_or_index)

Alert the innerHTML of the first cell in the table's first row:

alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);

Try it yourself »

Example

namedItem(name_or_id)

Alert the innerHTML of the cell with id="myTd" in the table's first row:

alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);

Try it yourself »

Example

Change the content of the first table cell:

var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML="NEW CONTENT";

Try it yourself »


Table Object Reference Table Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook