jQuery – get index of Selected element

jQuery provides index() method to find out index of any element. This method return value is an integer indicating position of the element.
   $('table tr').mouseover(function(){
   $('table tr').click(function(){
       var index = $('table tr').index(this);
       alert('You Clicked row ' + index);
<td>Row 1</td>
<td>Row 2</td>
<td>Row 3</td>
 – OR – 
$("div").click(function () {
  var index = $("div").index(this);
  $("span").text("That was div index #" + index);



<span>Click a div!</span>
<div>First div</div>
<div>Second div</div>
<div>Third div</div>

Live Demo 


Share this nice post:

One thought on “jQuery – get index of Selected element

Comments are closed.