How to find index of Selected element

jQuery provides index() method to find out index of any element. index() method returns an integer value that indicates position of the element. If the element is not found then it returns -1. The index value starts from 0 which means 1st element value will be 0.

See below code snippets which finds the index of the click tr element in the table.
$(document).ready(function(){
   $('table tr').mouseover(function(){
       $(this).css("cursor","pointer");
   });
   $('table tr').click(function(){
       var index = $('table tr').index(this);
       alert('You Clicked row ' + index);
    });
});
See live Demo and Code.

If you don't pass any argument in the index function then it will always return position of first element, which is 0.

Feel free to contact me for any help related to jQuery. I will gladly help you.
SHARE

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..