title: Uncaught TypeError: Cannot read property 'indexOf' of undefined in for loop
body: <p>I have the following code</p>
  
  <pre><code>var results = ["img", "some_class"];
  
  for ( var i = 0; i &lt;= results.length; i++ ){
  
          if (results[i] === "input"){
              elements = [];
  
          } else if (results[i].indexOf("class") &gt; -1 ){
  
              className = results[i];
              b = document.getElementsByClassName;
  
              for ( var i = 0; i &lt; b.length; i++ ){
                  elements.push(b[i]);
              }
  
          } else if (results[i].indexOf("id") &gt; -1 ){
  
              id = results[i];
              searchId = document.getElementById(id);
              elements.push(searchId);
              }
          }
  </code></pre>
  
  <p>I'm trying to check whether any of the elements in the <code>results</code> array contain the strings "class" or "id" but I get <code>Uncaught TypeError: Cannot read property 'indexOf' of undefined</code></p>
  
  <p>You can see that the <code>results</code> array does indeed contain "class" inside the second element but my code isn't playing ball.</p>
  
  <p>Any help would be much appreciated :)</p>