Table cells Verzameling

Definitie en gebruik

cells De verzameling geeft de verzameling van alle <td> of <th>-elementen in de tabel terug.

Note:Elements in the collection are sorted in the order they appear in the source code.

Tip:Gebruik rows Verzameling Geef de verzameling van alle <tr>-elementen in de tabel terug.

Tip:Please use insertRow() Methode Maak een nieuwe rij (<tr>) aan.

Tip:Please use deleteRow() Methode Verwijder rij.

Tip:Please use insertCell() Methode Create a new cell (<td>).

Tip:Please use deleteCell() method Delete cell.

See also:

HTML Reference Manual:HTML <td> tag

HTML Reference Manual:HTML <th> tag

JavaScript Reference Manual:HTML DOM TableData object

JavaScript Reference Manual:HTML DOM TableHeader object

Instance

Example 1

Display the number of cells in the first row:

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

Try it yourself

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);

Try it yourself

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);

Try it yourself

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);

Try it yourself

Example 5

Change the content of the first table cell:

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

Try it yourself

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:Returns null if the index number is out of range.

item(index)

Returns the <td> and/or <th> element at the specified index in the collection (starting from 0).

Note:Returns null if the index number is out of range.

namedItem(id)

Returns <td> and/or <th> elements from the collection with the specified id.

Note:Returns null if the id does not exist.

Technical details

DOM version: Core Level 2 Document Object
Return value:

HTMLCollection object, representing all <td> and/or <th> elements within the <tr> element.

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