title: Javascript TypeError: Cannot read property when nested inside a window.onload function
body: <p><strong>SOLVED:</strong> Will move to close post but for future reference here is what happened. The children div in the parent div I was referencing were dependent on an AJAX call. So I set an interval to keep checking if the element existed with...</p>
  
  <pre><code>var checkExist = setInterval(function() {
     if ($('parent').length) {
        // do work on child elements
        clearInterval(checkExist);
     }
  }, 100);
  </code></pre>
  
  <p><strong>ORIGINAL POST BELOW</strong></p>
  
  <p>I am getting into writing some scripts to make testing tasks simple. I need to wait to run some of these until the page is fully loaded. Naturally I nest my code in ...</p>
  
  <pre><code>window.onload = function () { code in here }
  </code></pre>
  
  <p>So I am trying to store an element in a var that I know exists and I know works without being nested in onload. The var is written like this.</p>
  
  <pre><code>var element = document.getElementById('userPanel').getElementsByTagName('div')[0];
  </code></pre>
  
  <p>I can run this in console and it returns the element I need. When I run my script nested in the onload function. It throws a type error. Is there some fundamental principle of javascript I am overlooking here? Thanks!</p>
  
  <p>For sake of clarity here is the whole code section glued together</p>
  
  <pre><code>    window.onload = function () {
  var element = document.getElementById('userPanel').getElementsByTagName('div')[0];
  }
  </code></pre>