TableRow cells collection
Definition and usage
cells
The collection returns all <td> or <th> Collection of elements.
Note:The elements in the collection are sorted in the order they appear in the source code.
Instance
Example 1
Display the number of cells in the first row:
var x = document.getElementById("myTable").rows[0].cells.length;
The result of x will be:
2
Example 2: [index]
Output the innerHTML of the first cell in the first row of the table:
alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
Example 3: item(index)
Output the innerHTML of the first cell in the first row of the table:
alert(document.getElementById("myTable").rows[0].cells.item(0).innerHTML);
Example 4: namedItem(id)
Output the innerHTML of the cell with id="myTd" in the first row of the table:
alert(document.getElementById("myTable").rows[0].cells.namedItem("myTd").innerHTML);
Example 5
Change the content of the first table cell:
var x = document.getElementById("myTable").rows[0].cells; x[0].innerHTML = "NEW CONTENT";
Syntax
tableObject.cells
Property
Property | Description |
---|---|
length |
Returns the number of <td> and/or <th> elements in the collection. Note:This property is read-only. |
Method
Method | Description |
---|---|
[index] |
Returns the <td> and/or <th> element at the specified index in the collection (starting from 0). Note:If the index number is out of range, it returns null. |
item(index) |
Returns the <td> and/or <th> element at the specified index in the collection (starting from 0). Note:If the index number is out of range, it returns null. |
namedItem(id) |
Returns <td> and/or <th> elements from the collection with the specified id. Note:If the id does not exist, it returns null. |
Technical Details
DOM Version: | Core Level 2 Document Object |
---|---|
Return Value: |
HTMLCollection Object, representing all <td> and/or <th> elements within the <tr> element. The elements in the collection are sorted in the order they appear in the source code. |
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |