title: How do I get the index of the element on a single click? (Pure JS)
body: <p>I am trying to get the elements index on the first click. I am not sure what is going on with my eventListener but it only seems to work after i have clicked once on the element.</p>
  
  <p>I tried changing the scope of the listener and debugging directly through "getListeners" of the element but to no avail.</p>
  
  <p>I also tried to separate the listeners instead of nesting it but then it jsut doesn't work. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const directory = document.querySelector('.directory')
    ,        card = document.getElementsByClassName('card')
    ;
  
  directory.addEventListener('click', () =&gt;
  {
    if (card)
    {
      for (let i = 0; i &lt; card.length; i++)
      {
        card[i].addEventListener('click', () =&gt;
        {
          console.log(i);
        });
      }
    }
  })</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.card {
    cursor: pointer;
    margin: 10px 0;
    text-align: center;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;main class="directory"&gt;
    &lt;div class="card"&gt;0&lt;/div&gt;
    &lt;div class="card"&gt;1&lt;/div&gt;
    &lt;div class="card"&gt;2&lt;/div&gt;
  &lt;/main&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>if the .card element is clicked then "i" should be printing to the console.</p>
  
  <p>However, always the first click produces no console print.</p>