tr cells Collection
Examples
Display the number of cells in the first row:
var x = document.getElementById("myTable").rows[0].cells.length;
xThe output is:
2
Try it Yourself »
Definition and Usage
The cells collection returns the number of all <td> or <th> elements in a table row.
Note:The elements in the collection are in the same order as in the source code.
Browser Support
| Collection | |||||
|---|---|---|---|---|---|
| cells | Yes | Yes | Yes | Yes | Yes |
Syntax
tableObject.cells
Properties
| Property | Description |
|---|---|
| length | Returns the number of <td> or <th> elements in the collection. Note:This property is read-only. |
Methods
| Method | Description |
|---|---|
| [name_or_index] | An integer that specifies the position of the element to retrieve (starting from 0). |
| item(name_or_index) | Returns the element at the specified index in the collection. |
| namedItem(name) | Returns the element with the specified name or index in the collection. |
Technical Details
| DOM Version: | Core Level 2 Document Object |
|---|---|
| Return Value: | HTMLCollection object, representing all <td> or <th> elements in <tr>. The elements in the collection are in the same order as in the source code. |
More Examples
Example
[index]
Alert the content of the first cell in the first row:
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
Try it Yourself »
Example
item(index)
Alert the content of the first cell in the first row:
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
Try it Yourself »
Example
namedItem(id)
Alert the content of the cell with id="myTd" in the first row:
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);
Try it Yourself »
Example
Modify the content of the first cell:
var x = document.getElementById("myTable").rows[0].cells;
x[0].innerHTML = "NEW CONTENT";
x[0].innerHTML = "NEW CONTENT";
Try it Yourself »
Other Extensions
tr Object