Angel Angel - 1 year ago 119
Javascript Question

Get current rowIndex of table in jQuery

My table cell gets highlighted when clicked. I need to find the rowIndex of highlighted cell.
I tried doing like this

$(".ui-state-highlight").index(); // Results to 0

I tried this too...


var row_index = $(this).parent().index('tr');

var col_index = $(this).index('tr:eq('+row_index+') td');

alert('Row # '+(row_index)+' Column # '+(col_index));

// Results : Row # -1 Column # -1

I went to through this post and tried the first answer, still couldn't get the result.

Answer Source

Try this,

   var row_index = $(this).parent().index();
   var col_index = $(this).index();

If you need the index of table contain td then you can change it to

var row_index = $(this).parent('table').index(); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download