title: toDataURL returns error "Uncaught TypeError: Cannot read property '0' of undefined "
body: <p>I'm new to javascript and am having an error. I'm sure I am skipping some basic concept... sorry. </p>
  
  <p>here is the problem. </p>
  
  <p>I use on my html this code:</p>
  
  <pre><code>&lt;div&gt;
  &lt;script type='text/javascript'&gt;
  var myCanvas = document.getElementsByTagName("canvas");
  document.write('&lt;img src="'+myCanvas[0].toDataURL("image/png")+'"/&gt;');
  
  &lt;/script&gt;&lt;/div&gt;
  </code></pre>
  
  <p>I have only one canvas on my document. The error I see in chrome is: </p>
  
  <pre><code>Uncaught TypeError: Cannot read property '0' of undefined sankey.html:128 (anonymous function)
  </code></pre>
  
  <p>If I enter </p>
  
  <pre><code>document.write('&lt;img src="'+myCanvas[0].toDataURL("image/png")+'"/&gt;');
  </code></pre>
  
  <p>(the exact same line) in the java script chrome console, it works! How is this possible?</p>