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
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
Share Improve this question asked Oct 7, 2013 at 12:25 user2767541user2767541 972 gold badges3 silver badges11 bronze badges 3- your intention is to use JQuery ?? – mpakbaz Commented Oct 7, 2013 at 12:27
- And where is your actual problem, what have you tried? – C3roe Commented Oct 7, 2013 at 12:27
- I want to remove tr from table – user2767541 Commented Oct 7, 2013 at 12:28
4 Answers
Reset to default 9Here 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.
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>
If you want to remove the row with id b
$("#b").remove()
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