Javascript Reference - HTML DOM TableRow sectionRowIndex Property








The sectionRowIndex property returns the position of a row in the rows collection of a <tbody>, <thead>, or <tfoot>.

Browser Support

sectionRowIndex Yes Yes Yes Yes Yes

Syntax

Return the sectionRowIndex property.

var v = tablerowObject.sectionRowIndex 

Return Value

A Number type value representing the position of the row in the rows collection of a tbody, thead, or tfoot.





Example

The following code shows how to get the position of the rows in the thead, tbody, and tfoot rows collection.


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

<table>
  <thead>
  <tr>
    <th>Row 1</th>
  </tr>
  <tr>
    <th>Row 2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>Row 3</td>
  </tr>
  <tr>
    <td>Row 4</td>
  </tr>
  </tbody>
  <tfoot>
  <tr>
    <td>Row 5</td>
  </tr>
  <tr>
    <td>Row 6</td>
  </tr>
  </tfoot>
</table>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementsByTagName("tr");
    var i;
    for (i = 0; i < x.length; i++) {
        console.log("The index of Row "+(i+1)+" is: "+x[i].sectionRowIndex);
    }
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to click on different rows to output their position.


<!DOCTYPE html>
<html>
<head>
<style>
table, td {<!--from w w  w.  j  av a  2  s.  c  o m-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
  <thead>
  <tr onclick="myFunction(this)">
    <th>Click to show sectionRowIndex</th>
  </tr>
  <tr onclick="myFunction(this)">
    <th>Click to show sectionRowIndex</th>
  </tr>
  </thead>
  <tbody>
  <tr onclick="myFunction(this)">
    <td>Click to show sectionRowIndex</td>
  </tr>
  <tr onclick="myFunction(this)">
    <td>Click to show sectionRowIndex</td>
  </tr>
  </tbody>
  <tfoot>
  <tr onclick="myFunction(this)">
    <td>Click to show sectionRowIndex</td>
  </tr>
  <tr onclick="myFunction(this)">
    <td>Click to show sectionRowIndex</td>
  </tr>
  </tfoot>
</table>

<script>
function myFunction(x) {
    console.log("Section row index is: " + x.sectionRowIndex);
}
</script>

</body>
</html>

The code above is rendered as follows: