TableRow rowIndex Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableRow

Description

The rowIndex property returns the position of a row in the rows collection of a table.

Return Value

A Number, representing the position of the row in the rows collection of a table

The following code shows how to Click on different rows to alert their index position:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>//from   w w w  .j a v a2 s . c om
</head>
<body>

<table>
  <tr>
    <td>Row 1</td>
  </tr>
  <tr>
    <td>Row 2</td>
  </tr>
  <tr>
    <td>Row 3</td>
  </tr>
</table>

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

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

<script>
function myFunction() {
    var x = document.getElementsByTagName("tr");
    var txt = "";
    var i;
    for (i = 0; i < x.length;i++) {
        txt = txt + "The index of Row "+(i+1)+" is: "+x[i].rowIndex+"<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials