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

TableRow cells Collection

TableRow Object Reference TableRow 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 row.

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


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 an 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 »


TableRow Object Reference TableRow Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE