Easy Tutorial
❮ Coll Table Cells Jsref Toexponential ❯

tr cells Collection

tr Object

Example

Display the number of cells in the first row:

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

x Output result:

2

Definition and Usage

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

Note: The order of elements in the collection is the same as in the source code.


Browser Support

Collection Chrome Edge Firefox Safari Opera
cells Yes Yes Yes Yes Yes

Syntax

tableObject.cells

Properties

Property Description
length Returns the number of <td> or <th> elements in the collection. <br> <br> Note: This property is read-only.

Methods

Method Description
[name_or_index] An integer specifying the element retrieval position (starting from 0)
item(name_or_index) Returns the element at the specified index in the collection
namedItem(name) Returns the element at the specified name index in the collection

Technical Details

DOM Version: Core Level 2 Document Object
Return Value: HTMLCollection object, representing all <td> or <th> elements in a <tr>. The order of elements in the collection is the same as in the source code.

More Examples

Example

[index]

Pop up the content of the first cell in the first row:

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

Example

item(index)

Pop up the content of the first cell in the first row:

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

Example

namedItem(id)

Pop up the content of the cell with id="myTd" in the first row:

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

Example

Modify the content of the first cell:

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

tr Object

❮ Coll Table Cells Jsref Toexponential ❯