title: Javascript how to get the id of the button that was just clicked
body: <p>I have some Javascript, and in one function, I need to get the id of the button that was just clicked. I tried a few things, like this:</p>
  
  <pre><code>var e = window.event,
  btn = e.target || e.srcElement;
  alert(btn.id);
  </code></pre>
  
  <p>but that just causes my program to crash.</p>
  
  <p>And this:</p>
  
  <pre><code>var buttons = document.getElementsByTagName("button");
  var buttonsCount = buttons.length;
  for (var i = 0; i &lt;= buttonsCount; i += 1) {
      buttons[i].onclick = function(e) {
          alert(this.id);
      };
  }
  </code></pre>
  
  <p>that also just causes my program to crash.</p>
  
  <p>And this:</p>
  
  <pre><code>function functionName(clicked_id) {
      alert(clicked_id);
  }
  </code></pre>
  
  <p>that just alerts "undefined".</p>
  
  <p>If I could have some debugging help or just another way to do it, that would be helpful, but it can't have jquery.</p>
  
  <p>Thanks</p>