title: Current index of a button array
body: <p>How do I return the index of only the button that is clicked?</p>
  
  <p>Here's the code that got me worried:</p>
  
  <pre><code>window.onload = function(){
  
  var    description = document.getElementsByClassName('description'),
          buttons = document.getElementsByClassName('button');
  
  var currD = 0; // this var stands for the current description that should be shown
  
  var show = function(){
  
      for( var i = 0; i &lt; description.length; i++ ){
  
          if( i !== currD ){
              description[i].style.display='none';
          }
  
          else if( i === currD ){
              description[i].style.display='block';   
          }
  
      }
  
  };
  
    for (var i = 0; i &lt; buttons.length; i++){
      buttons[i].addEventListener('click', function(){
          currD = i;
          console.log(i);
      });
  }
  
      window.setInterval(show,300);
  };
  </code></pre>
  
  <p>Every time I click the button the for loop return the last element.</p>
  
  <p>Since I didn't have many buttons on this page I went for the unproficient old way which is:</p>
  
  <pre><code>window.onload = function(){
  
  var description = document.getElementsByClassName('description'),
      buttons = document.getElementsByClassName('button');
  
      var currD = 0; // this var stands for the current description that should be shown
  
      var show = function(){
  
          for( var i = 0; i &lt; description.length; i++ ){
  
              if( i !== currD ){
                  description[i].style.display='none';
              }
  
              else if( i === currD ){
                  description[i].style.display='block';   
              }
  
          }
  
      };
  
  buttons[0].addEventListener('click', function(){
      currD = 0;
  });
  
  buttons[1].addEventListener('click', function(){
      currD = 1;
  });
  
  buttons[2].addEventListener('click', function(){
      currD = 2;
  });
  
          window.setInterval(show,300);
      };
  </code></pre>
  
  <p>This works but if I want to add more buttons it'd be a loss of time setting all the event listeners.</p>