Walrus
Walrus

Reputation: 20444

JQuery find nth child and display

Using JQuery how do we find what the current nth:child the element being clicked is and alert that number.

$('.bouncetabs a').click(function(){
        alert($(this + ':nth-child'))
        return false
    });

Obviously that does not work. How do we do it?

Upvotes: 2

Views: 1849

Answers (1)

Alex Peattie
Alex Peattie

Reputation: 27647

I think you want the .index method:

var selector = '.bouncetabs a';

$(selector).click(function(){ 
  //just .index() may work too, depending on the html
  alert($(this).index(selector)); 
  return false;
});

Upvotes: 4

Related Questions