title: Implementing document.getElementById in javascript
body: <p>I'm trying to implement native <code>document.getElementById</code> in javascript. I've implemented <code>document.getElementsByClassName</code> in javascript.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function getElementsByClassName (className) {
    var nodeList = [];
    function test(node) {
        if (node.classList &amp;&amp; node.classList.contains(className)) {
          nodeList.push(node);
        }
        
        for (var index = 0; index &lt; node.childNodes.length; index++) {
          test(node.childNodes[index]);
        }
        
        return nodeList;
    }
    
      test(document.body);
      
    return nodeList;
  };
  
  // Fails here.
  function getElementById(className) {
      const result = [];
      
      function getEachIDNode(node) {
          if(node.contains(className)) {
              return node;
          }
  
          for(let i=0; i&lt;node.childNodes.length; i++) {
              getEachIDNode(node.childNodes[i]);
          }
  
      }
  
      getEachIDNode(document.body);
  }
  
  console.log(getElementsByClassName('winner'));
  console.log(getElementById('test'));</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  &lt;table&gt;      
          &lt;tr id="test"&gt;
              &lt;td&gt;#&lt;/td&gt;
              &lt;td class="winner"&gt;aa&lt;/td&gt;
              &lt;td&gt;bb&lt;/td&gt;
              &lt;td&gt;cc&lt;/td&gt;
              &lt;td&gt;dd&lt;/td&gt;
          &lt;/tr&gt;
     &lt;/table&gt;
  
     &lt;table&gt;      
          &lt;tr&gt;
              &lt;td&gt;#&lt;/td&gt;
              &lt;td class="winner"&gt;aa&lt;/td&gt;
              &lt;td&gt;bb&lt;/td&gt;
              &lt;td&gt;cc&lt;/td&gt;
              &lt;td&gt;dd&lt;/td&gt;
          &lt;/tr&gt;
     &lt;/table&gt;
  
     &lt;table&gt;      
          &lt;tr&gt;
              &lt;td&gt;#&lt;/td&gt;
              &lt;td class="winner"&gt;dd&lt;/td&gt;
              &lt;td&gt;cc&lt;/td&gt;
              &lt;td&gt;bb&lt;/td&gt;
              &lt;td&gt;aa&lt;/td&gt;
          &lt;/tr&gt;
     &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>I'm trying to understand how I can check if a node has an attribute ID.</strong> </p>
  
  <p>Can someone enlighten me?</p>