noname noname - 2 years ago 121
HTML Question

Find Row Index in HTML Table Row DOM Element

I have a table set up now in just Knockout and jQuery and I need to be able to find the row index of a row that is clicked for selection. I have it set up so that my table is populated by a Knockout observable array and my table row click events are handled by the following function:

$("#table tr").live("click", callbackFunc(position));


But as you can see I need the position of the row that is clicked on to be passed in to the function for it to work. The last thing I tried was:

$("tr",$(this).parent("#table")).index(this)


But that only returned -1.

Anyone know of a solution?

Answer Source

you can do something like this in jquery

$("#table tr").on("click", function()
{
$(this).prevAll("tr").size();        //this will return all tr before current tr 
                                     //i.e current row index

} );

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download