简体   繁体   中英

How to get table row by using id in html?

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

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>

如果要删除 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM