javascript - How to get the id of next tr in table using jquery? - Stack Overflow

admin2025-04-22  0

<table>
<tr id ="tr_id_1">
    <td >
        Blah
    </td
    <td>
        Blah
    </td>
</tr>
<tr id ="tr_id_2">
    <td>
        Blah
    </td>
    <td>
    Blah
    </td>
</tr>

</table>

i have got the id of first tr using jquery

var first_tr_id = tr_id_1     // id of first tr

now by using this id how to get the id of next tr i have tried like this

var nextId = ('#'+first_tr_id ).next("tr").attr("id");

but its giving ("#" + row_id).next is not a function error ..

<table>
<tr id ="tr_id_1">
    <td >
        Blah
    </td
    <td>
        Blah
    </td>
</tr>
<tr id ="tr_id_2">
    <td>
        Blah
    </td>
    <td>
    Blah
    </td>
</tr>

</table>

i have got the id of first tr using jquery

var first_tr_id = tr_id_1     // id of first tr

now by using this id how to get the id of next tr i have tried like this

var nextId = ('#'+first_tr_id ).next("tr").attr("id");

but its giving ("#" + row_id).next is not a function error ..

Share Improve this question edited Jun 17, 2011 at 9:30 Clement Herreman 10.5k4 gold badges37 silver badges57 bronze badges asked Jun 17, 2011 at 9:27 HussyHussy 2,0294 gold badges25 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You code lacks the jQuery $

var nextId = $('#'+first_tr_id ).next("tr").attr("id");

Also an id is a string, so you should set your variable like so :

var first_tr_id = 'tr_id_1';    // id of first tr

you should try in this way..

var nextId = $('#'+first_tr_id ).next("tr").attr("id");

You have a problem in assigning a variable:

var first_tr_id = tr_id_1 

it should be inside quote

var first_tr_id = "tr_id_1"

Also, to get all the IDs, you can use the following code:

$("table tr").each(function(index) {
   alert($(this).id());
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745297553a295061.html

最新回复(0)