How to get number of elements of a class before a certain element
- by David Shaikh
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