title: Applying onClick to Element Array in a For Loop
body: <p>I am trying to apply an onClick event to an array of elements in a document, like this:</p>
  
  <pre><code>for (var i = 0; i &lt; myElems.length; i++)
       myElems[i].onClick = "someFunction(this)";
  </code></pre>
  
  <p>The code is placed inside of an init() function for the onLoad event of the body tag. I notice that when the document loads, the functions wont work.</p>
  
  <p>I've noticed, that if I add an alert() to tell me if the function is the problem:</p>
  
  <pre><code>for (var i = 0; i &lt; myElems.length; i++)
       myElems[i].onClick = "alert('It worked!')";
  </code></pre>
  
  <p>The document will load and perform the alert for all of the elements, without ever taking into consideration whether or not I actually clicked the element.</p>
  
  <p>What am I doing wrong?</p>