title: How do I use variables in document.getElementById
body: <p>I have this code which I've been trying to fix for hours.</p>
  
  <pre><code>&lt;script language="JavaScript"&gt;
      &lt;!--
  
      function generate(){
      var titels = new Array();
      var i = 0;
      for(i;i&lt;9;i++){
      var test = 'h1-0'+ i;
          titels[i] = document.getElementById(test).textContent;
      }
  
      document.getElementById("uitkomst").value = titels[1];      
      }
      --&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>This gives me the error </p>
  
  <pre><code>TypeError: document.getElementById(...) is null
  titels[i] = document.getElementById(test).textContent;
  </code></pre>
  
  <p>But when I change 'h1-0'+i  by 'h1-0'+5 it does work and I don't get an error, so how do I fix this? Why is Javascript so annoying when using variables?</p>