Reputation: 97
How to get tr count from html table by using Id.Please check my code below
<table id="tableId">
<tr id="a">
<td>a</td>
</tr>
<tr id="b">
<td>b</td>
</tr>
Now I want to find rowNumber by using row Id.for example my rowId is 'b' I want to find rowNumber for this Id.Any one help me
Upvotes: 6
Views: 54367
Reputation: 153
If you have the Id within any cell, then the below should work
document.getElementById("myID "+tempIdx).parentNode.parentNode.rowIndex;
provided table is in the below format
<table>
<tr>
<td>
<p id="myID 1" />
</td>
<td>xxx</td>
</tr>
<td>yyy</td>
<td>
<p id="myID 2" />
</td>
</tr>
<td>
<p id="myID 3" />
</td>
</tr>
</table>
The first .parentNode will give the CELL the second will give the ROW, then get the INDEX from the ROW
Upvotes: 0
Reputation: 775
It's very using with jquery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var rowCount = $('table#tableId tr:#b').index() + 1;
alert(rowCount);
$("#b").remove() // For Remove b Row (tr)
});
</script>
Your HTML
<table id="tableId">
<tr id="a">
<td>a</td>
</tr>
<tr id="b">
<td>b</td>
</tr>
</table>
Upvotes: 1
Reputation: 665
Here you go
var table = document.getElementById("tableId");
var rowIndex = document.getElementById("b").rowIndex;
table.deleteRow(rowIndex);
Added the code for deletion, as requested in the comments below.
Upvotes: 9