Table rows collection

Definition and Usage

rows The collection returns a set of all <tr> elements in the table.

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

Tip:Use insertRow() method Create a new row (<tr>).

Tip:Use deleteRow() method Delete row.

Tip:Use insertCell() method Create a new cell (<td>).

Tip:Use deleteCell() method Delete cell.

Tip:Use cells collection Returns a collection of all <td> or <th> elements in the table.

See also:

HTML Reference Manual:HTML <tr> Tag

JavaScript Reference Manual:HTML DOM TableRow Object

Instance

Example 1

Find out how many rows are in the table:

var x = document.getElementById("myTable").rows.length;

Try It Yourself

The result of x will be:

2

Example 2: [index]

Prompt the innerHTML of the first <tr> element (index 0) in the table:

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

Try It Yourself

Example 3: item(index)

Prompt the innerHTML of the first <tr> element (index 0) in the table:

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

Try It Yourself

Example 4: namedItem(id)

Prompt the innerHTML of the <tr> element with id="myRow" in the table:

alert(document.getElementById("myTable").rows.namedItem("myRow").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.rows

Property

Property Description
length

Returns the number of <tr> elements in the collection.

Note:This property is read-only.

Method

Method Description
[index]

Returns the <tr> element at the specified index in the collection (starting from 0).

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

item(index)

Returns the <tr> element at the specified index in the collection (starting from 0).

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

namedItem(id)

Returns the <tr> element with the specified id in the collection.

Note:Returns null if the id does not exist.

Technical Details

DOM Version: Core Level 2 Document Object
Return value:

The HTMLCollection object represents all <tr> elements within the <table> 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