Table rows Collection - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Table


The rows collection returns all <tr> elements in a table sorted as they appear in the HTML code.


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


[index] <tr> element with the specified index (starts at 0).
item(index) <tr> element with the specified index (starts at 0).
namedItem(id) <tr> element with the specified id.

Technical Details

Return Value:

An HTMLCollection Object, representing all <tr> elements in the <table> element.

The following code shows how to Find out how many rows there are in a table:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
table, td {
    border: 1px solid black;
</style>// www  .ja  v a  2 s  . co m

<table id="myTable">
    <td>cell 1</td>
    <td>cell 2</td>
    <td>cell 3</td>
    <td>cell 4</td>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

function myFunction() {
    var x = document.getElementById("myTable").rows.length;
    document.getElementById("demo").innerHTML = "Found " + x + " tr elements in the table.";


Related Tutorials