HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

Table rows Collection

Table Object Reference Table Object

Example

Show the number of rows in a table:

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

The result of x will be:

2

Try it yourself »

Definition and Usage

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

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

Tip: Use the insertRow() method to create a new row (<tr>).

Tip: Use the deleteRow() method to remove a row.

Tip: Use the insertCell() method to create a new cell (<td>).

Tip: Use the deleteCell() method to delete a cell.

Tip: Use the cells collection to return a collection of all <tr> elements in a table.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The rows collection is supported in all major browsers.


Syntax

tableObject.rows

Properties

Property Description
length Returns the number of <tr> elements in the collection

Methods

Method Description
[name_or_index] A string or integer that specifies the element to retrieve (index starts at 0)
item(name_or_index) Returns the element from the collection with the specified index/name/id
namedItem(name_or_id) Returns the element from the collection with the specified name (name or id attribute)


More Examples

Example

[name_or_index]

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

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

Try it yourself »

Example

item(name_or_index)

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

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

Try it yourself »

Example

namedItem(name_or_id)

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

alert(document.getElementById("myTable").rows.namedItem("myRow").innerHTML);

Try it yourself »

Example

Change the content of the first table cell:

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

Try it yourself »


Table Object Reference Table Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE