title: javascript's document.getElementById returns null, but works from the console
body: <p>I tried to access and change properties of some element, from the onerror event of some image.
  But... when I load the page, I get Uncaught TypeError: Cannot call method ... of null. 
  I know that usually, this happens because the element hasn't loaded yet.</p>
  
  <p>I added delay of 10 seconds, using setTimeout. Now, I loaded the code, and successfully reached the element via the console (in both Chrome and Firefox). I stil got the TypeError message.</p>
  
  <p>Note that the code from the 'onerror', works fine from the console.</p>
  
  <p>I tried to do what I wanted in another way: I tried to redefine a global function from the "onerror" property of the image. But again: the global function didn't change. It isn't a matter of time, and the code worked perfectly from the console (the same onerror code, redefined the global function successfully from the console in both Chrome and FF). </p>
  
  <p>I will bring here some examples of tries I did (I rewrited them quickly, hopefully without any typo):</p>
  
  <pre><code>&lt;img src='' onerror='setTimeout(function(){document.getElementById(someElementString).addEventListener(clickString,function(){alert(1234);});},6000);' height=100 width=100 /&gt;
  
  &lt;img src='' onerror='function(){setTimeout(function(){document.getElementById(someElementString).addEventListener(clickString,function(){alert(1234);});},6000);};' height=100 width=100 /&gt;
  
  &lt;img src='' onerror='f = function(){setTimeout(function(){document.getElementById(someElementString).addEventListener(clickString,function(){alert(1234);});},6000);}; f();' height=100 width=100 /&gt;
  
  &lt;img src='' onerror='setTimeout(function(){window.globalFunction = function(e){alert(656565);};},6000);' height=
  200 width=100 /&gt;
  </code></pre>
  
  <p>I also wrapped the onerror as a function but without success.</p>
  
  <p>If I replace the onerror code with a simple alert, it works; And again: it works also from the console, even before the code onerror code runs.</p>
  
  <p>What can be the reason for this?
  Any help will be appreciated!</p>