title: I am trying to use vanilla JS to highlight clicked anchor and remove the previously highlighted anchor
body: <p>This is my first post and I don't know what I am doing yet, so please bear with me.</p>
  <p>I am trying to use vanilla JS to highlight a clicked anchor and remove the previously highlighted anchor.</p>
  <p>I am not sure why it is not working but I am new at this.</p>
  <p><a href="https://codepen.io/rblok4/pen/LYGLMOJ" rel="nofollow noreferrer">My CodePen</a></p>
  <p>below is just the JS. .nav-link is the link class. active is the class that should manipulate it after the click.</p>
  <pre><code>window.onload = afterClick;
  
  
  function afterClick(){
   //set an on off array to toggle. 
    let linkClass = document.querySelectorAll(&quot;.nav-link&quot;);
    
    linkClass.forEach(link =&gt; link.addEventListener(&quot;click&quot;, function(){
    if (link.classList.contains(&quot;active&quot;)) {
      link.classList.remove(&quot;active&quot;);  
    link.classList.add(&quot;active&quot;);
    }}));  
  }
  </code></pre>
  <p>Thank you!</p>