Question

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

No correct solution

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top