Replacing onclick in html

Hi.

I am using this script and want to replace the onclick event in the html by a pure javascript script:

https://jsfiddle.net/uu152uu9/

I cannot use the javascript in the html.

I have tried this modification:

<header>
  <nav>
    <ul id="menu">
      <li><a href="#">home</a></li>
      <li><a href="#">about</a></li>
      <li><a href="#">service</a></li>
      <li><a href="#">profile</a></li>
      <li><a href="#">portfolio</a></li>
      <li><a href="#">contact</a></li>
    </ul>
  </nav>
</header>

document.getElementById("menu").onclick = function() {
  var elems = document.querySelectorAll(".active");
  [].forEach.call(elems, function(el) {
    el.classList.remove("active");
  });
  e.target.className = "active";
}

But it does not work…

It worked:

document.getElementById("menu").onclick = function(e) {
  var elems = document.querySelectorAll(".active");
  [].forEach.call(elems, function(el) {
    el.classList.remove("active");
  });
  e.target.className = "active";
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.