Alex Rivera | Logout

How to find the index of a row in a table using jquery

Asked 2009-01-22T16:42:42.917
24

I'm trying to find the index of a row in a table. I'm trying to use the following code, but I seem to get an index of -1.

$(document).ready(function()
{
    $("tr").click(function (){
        var index = $("table").index($(this));
        $("span").text("That was row index #" + index);
    });
});

With html that looks like this;

<table>
<tbody>
    <tr><td>click</td></tr>
    <tr><td>click</td></tr>
    <tr><td>click</td></tr>
    <tr><td>click</td></tr>
</tbody>

Thanks

Edit
Report

1 Answer

3

Based on Robs answer to find index in specific table, this worked for me.

var index = $('tr', $(this).closest("table")).index(this);
answered 2013-01-04T15:42:58.590

Your Answer