title: JavaScript addEventListener 'click''
body: <p>I struggling with very basic thing</p>
  
  <p>I am trying to make this side-nav appear and disappear with JavaScript. The code below only activates the nav bar but doesn't deactivate it. Tried all options but I don't know how to call function to close nav-bar.</p>
  
  <p>Thank you</p>
  
  <pre><code>HTML
  
  
      &lt;div id="mySidenav" class="sidenav"&gt;
       &lt;a href="#" class="closebtn"&gt;&amp;times;&lt;/a&gt;
       &lt;a href="#"&gt;About&lt;/a&gt;
       &lt;a href="#"&gt;Services&lt;/a&gt;
       &lt;a href="#"&gt;Clients&lt;/a&gt;
       &lt;a href="#"&gt;Contact&lt;/a&gt;
      &lt;/div&gt;
  
       &lt;!-- Use any element to open the sidenav --&gt;
        &lt;span id="MyElement"&gt;&amp;times;&lt;/span&gt;
  
  
  JS
  
  
          &lt;script type="text/javascript"&gt;
      function changeClass() {
          document.getElementById("mySidenav").style.width = "250px";
  
      }
      window.onload = function() {
  document.getElementById("MyElement").addEventListener('click',changeClass);
      }
  &lt;/script&gt;
  
  
      &lt;/body&gt;
  
      &lt;/html&gt;
  </code></pre>