title: Apply "onclick" to all elements in an iFrame
body: <p>How do I use the JavaScript DOM to apply <code>onclick</code> events to links inside of an <code>iframe</code>?</p>
  
  <p>Here's what I'm trying that isn't working:</p>
  
  <pre><code>document.getElementById('myIframe').contentDocument.getElementsByTagName('a').onclick = function();
  </code></pre>
  
  <p>No errors seem to be thrown, and I have complete control of the stuff in the <code>iframe</code>.</p>
  
  <p>Here is some code to test and see if I can at least count how many <code>div</code>'s are in my <code>iframe</code>.</p>
  
  <pre><code>// access body
  var docBody = document.getElementsByTagName("body")[0];
  
  // create and load iframe element
  var embed_results = document.createElement('iframe');
  embed_results.id = "myIframe";
  embed_results.setAttribute("src", "http://www.mysite.com/syndication/php/embed.php");
  
  // append to body
  docBody.appendChild(embed_results);
  
  // count the divs in iframe and alert   
  alert(document.getElementById("myIframe").contentDocument.getElementsByTagName('div').length);
  </code></pre>