How to get number of elements of a class before a certain element
Posted
by
David Shaikh
on Stack Overflow
See other posts from Stack Overflow
or by David Shaikh
Published on 2012-09-06T18:14:56Z
Indexed on
2012/09/06
21:38 UTC
Read the original article
Hit count: 166
jQuery
I want to know how many elements of a certain class appear in the DOM before an element that let's say has been clicked on.
<html>
<div class="a">
</div>
<div class="b">
<div class="a">
</div>
</div>
<button>CLick me!</button>
<div class="a">
</div>
</html>
So in the previous DOM tree if the element to be clicked is the button, and Im looking for divs with class "a" it should return 2, even though in the whole tree there are 3, but "before" the button there are only 2.
How could I do that?
Thanks
© Stack Overflow or respective owner