The rowIndex
property returns
the position of a row in the rows collection of a table.
rowIndex |
Yes | Yes | Yes | Yes | Yes |
Return the rowIndex property.
var v = tablerowObject.rowIndex
A Number type value representing the position of the row in the rows collection of a table.
The following code shows how to click on different rows to output their index position.
<!DOCTYPE html>
<html>
<head>
<style>
table, td {<!--from w w w . j a v a 2 s. c o m-->
border:1px solid black;
}
</style>
</head>
<body>
<table>
<tr onclick="myFunction(this)">
<td>Click to show rowIndex</td>
</tr>
<tr onclick="myFunction(this)">
<td>Click to show rowIndex</td>
</tr>
<tr onclick="myFunction(this)">
<td>Click to show rowIndex</td>
</tr>
</table>
<script>
function myFunction(x) {
console.log("Row index is: " + x.rowIndex);
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the index position of each row in a table.
<!DOCTYPE html>
<html>
<head>
<style>
table, td {<!--from ww w. ja va 2s . c om-->
border: 1px solid black;
}
</style>
</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 i;
for (i = 0; i < x.length;i++) {
console.log("The index of Row "+(i+1)+" is: "+x[i].rowIndex);
}
}
</script>
</body>
</html>
The code above is rendered as follows: