title: Cannot read property 'addEventListener' of null error in javascript
body: <p>I am trying to animate open (+) and close (x) buttons for main navigation. It's not working properly and I am getting this error.</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'addEventListener' of null</p>
  </blockquote>
  
  <p><strong>JavaScript</strong></p>
  
  <pre><code>document.querySelector( "#nav-toggle" ).addEventListener( "click", function() {
    this.classList.toggle( "active" );
  });
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;a id="nav-toggle"   href="#navi" data-toggle="collapse" 
   data-target="#bs-example-navbar-collapse-1"&gt;
      &lt;span&gt;&lt;/span&gt;
  &lt;/a&gt;
  </code></pre>