title: get id of clicked button javascript
body: <p>I'm attempting to simply get the id of a clicked button and use alert to display it. The code below trys to <code>addEventListener</code>, if not falls back to <code>attachEvent</code>. To get the ID, Im trying to pass <code>this.id</code> as a parameter to <code>myFunction</code>. but this is not working. I've seen plenty of great solutions using Jquery and the likes, but unfortunately I have to use only plain JS for this. How do I get the ID of a clicked button and store it in a variable to be displayed?</p>
  
  <pre><code>function addEvents()
  {
      var buttonArray=document.getElementsByClassName('mainButton');
      for(i=0; i &lt; buttonArray.length; i++)
      {
          if (document.addEventListener) {
          buttonArray[i].addEventListener("click", myFunction(this.id));
          } else if (document.attachEvent) {
          buttonArray[i].attachEvent("onclick", myFunction(this.id));
  }
  
  function myFunction(clickedId) {
      alert("Button" + clickedId + "was clicked.");
  } 
  }
      }
  </code></pre>