How do I get the index of clicked item in the code below?
$('selector').click(function (event) {
// get index in collection of the clicked item ...
});
With Firebug I saw this: jQuery151017197709735298827: 2
(I've clicked in the second element).
jsfiddle
Siblings
$(this).index()
can be used to get the index of the clicked element if the elements are siblings.Not siblings
Pass the selector to the
index(selector)
.Example:
Find the index of the
<a>
element that is clicked.Fiddle
Just do this way:-
OR
This will alert the index of the clicked selector (starting with 0 for the first):
if you are using .bind(this), try this:
let index = Array.from(evt.target.parentElement.children).indexOf(evt.target);