How to get number of elements of a class before a certain element
<p>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.</p>

<pre><code>&lt;html&gt;
  &lt;div class="a"&gt;
  &lt;/div&gt;
  &lt;div class="b"&gt;
      &lt;div class="a"&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;button&gt;CLick me!&lt;/button&gt;
  &lt;div class="a"&gt;
  &lt;/div&gt;
&lt;/html&gt;
</code></pre>

<p>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.</p>

<p>How could I do that?</p>

<p>Thanks</p>

<p>[EDIT]
The search of elements starts at the begining of the DOM tree until the clicked element. Don't assume that structure, where the elemets are sibilings of the clicked element, it can have any kind of structture.</p>
