Bhushan Bhushan - 1 year ago 109
jQuery Question

How to find out which table row was clicked using jQuery?

I am using 'live' function to do some on-click stuff on table rows, i.e.

$("tr").live('click',function() {
alert('Some table row is clicked');

I want to find out which row is clicked and using
, give some custom alert based upon that. Can anyone tell me how to do it?

Thanks a lot.


Is there a way by which I can refer to the elements of the clicked row inside the function?

Answer Source
$("tr").live('click', function() {
    if ( == "foo") {
        alert('the tr with the foo id was clicked');

If you want to check which row number, use index:

$("tr").live('click', function() {
   if $(this).index() === 2) {
     alert('The third row was clicked'); // Yes the third as it's zero base index

Live demo


$("tr").live('click', function() {
    // "this" is the clicked <tr> element
    // $(this).find('td span') is the spans inside a td inside the clicked <tr>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download