Table cellsCollection
Table Object
Definition and Usage
The cells collection returns all <td> or <th> elements in a table.
Syntax
tableObject.cells
Properties
| Property | Description |
|---|---|
| length | Returns the number of <td> or <th> elements in the collection. |
Methods
| Method | Description |
|---|---|
| [name_or_index] | An integer that specifies the position of the element to retrieve (starts at 0). |
| item(name_or_index) | Returns the element at the specified index in the collection. |
| namedItem(name) | Returns the element in the collection with the specified name or index. |
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the cells collection.
Example
Example
The following example uses cells to display the content of the first cell:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
<script>
function displayResult(){
alert(document.getElementById("myTable").rows[0].cells.length);
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">Show the number of cells in the first row</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
<script>
function displayResult(){
alert(document.getElementById("myTable").rows[0].cells.length);
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">Show the number of cells in the first row</button>
</body>
</html>
Try it Yourself »
More Examples
Pop up the content of the first row
Align cell content in table rows
Vertical alignment of cell content in table rows
Align the content of a single cell
Vertical alignment of cell content
Change the content of a table cell
Table Object Other Extensions