title: Conditionally open links in a new tab
body: <p>My goal is to make these links open in a new tab <strong>only</strong> if the check box is ticked. </p>
  
  <p>Why is my anchor.getAttribute not a function if I change getElementByID to getElementsByClassName?</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt; 
      &lt;head&gt; &lt;/head&gt;
      &lt;title&gt; &lt;/title&gt;
      &lt;body&gt;
          &lt;input id="checkr" type="checkbox"&gt;Open in New Window&lt;/input&gt;
          &lt;br /&gt;
          &lt;a href="http://www.google.com" class="linker"&gt;Google&lt;/a&gt; &lt;br&gt;
          &lt;a href="http://www.w3schools.com" class="linker"&gt;W3 Schools&lt;/a&gt; &lt;br&gt;
          &lt;a href="http://www.twitch.tv" class="linker"&gt;Twitch&lt;/a&gt; &lt;br&gt;
  
          &lt;script&gt;
              var checkr = document.getElementById('checkr');
              var anchor = document.getElementsByClassName('linker');
              var link = anchor.getAttribute('href');
  
              function OpenWindow(href) {
                  if (checkr.checked) {
                      window.open(href, '_blank');
                  } else {
                      window.open(href, '_self');
                  }
              }
              anchor.onclick = function() {
                  OpenWindow(link);
                  return false;
              };
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>