jquery - How can I check the number of the item that was clicked -
let's have many items click event (all have same element name , possibly same class).
<a>a</a> <a>b</a> <a>c</a> <a>d</a>
then have method triggered on click event. how can check number of item clicked?
in example if 'c' clicked should 3 answer.
you can use .prevall()
:
$('a').click(function() { alert($(this).prevall('a').length + 1); });
or .index()
:
$('a').click(function() { alert($(this).index() + 1); });
Comments
Post a Comment