0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nobody
  • 7,803
  • 11
  • 56
  • 91
  • Does `callbackFunc(position)` return a function? If not, you're invoking `callbackFunc` when the click handler is bound, not when the click event is fired. – Matt Ball Jun 18 '13 at 16:33
  • I should have clarified! Yes it does return a function, thanks for pointing that out. – nobody Jun 18 '13 at 16:35

3 Answers3

2

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

Manav Dewan
  • 161
  • 5
0

Try this instead:

$("#table").index(this)

Hope this will fix your issue.

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
0

You should use .index() method of jQuery like this

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

function callbackFunc(){
   var index = $(this).parent().index(this);
}
U.P
  • 7,357
  • 7
  • 39
  • 61