Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
AddEventListener is not working for one element

AddEventListener is working fine for the first function but not triggering the second one, not even showing in the console.log. Please see where I'm going wrong?

    <ul id="nv">
      <li class="nvItem"><a href="#">Home</a></li>
      <li class="nvItem"><a href="#the-story">Story</a></li>
      <li class="nvItem"><a href="#comic-book">Comic</a></li>
      <li class="nvItem"><a href="#creatures">Creatures</a></li>
      <li class="nvItem"><a href="#the-path">Path</a></li>
      <li class="nvItem"><a href="#artists">Nikal</a></li>                      
    </ul>

Please ignore the id mn

const navigation = document.getElementById("nv");
const menu = document.getElementById("mn");
const navItems = document.getElementsByClassName("nvItem")

menu.addEventListener("click", () => {
  navigation.style.setProperty("--childenNumber", navigation.children.length);

  navigation.classList.toggle("active");
  menu.classList.toggle("active");
  console.log("---Active---");
});

navItems.addEventListener("click", () => {
    navigation.classList.toggle("nonActive");
    menu.classList.toggle("nonActive");
    console.log("---Deactive---");
  });
  
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You should take some time and read this about event listeners: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

They can not be attached to NodeLists https://developer.mozilla.org/en-US/docs/Web/API/NodeList

We can get around this by looping through the Nodes and applying an EventListener to each node.

Because you have tags you may have some side effects. I would recommend pointer-events: none or some other kind of prevention on the to stop default behavior.

const navigation = document.getElementById("nv");
const menu = document.getElementById("mn");
const navItems = document.getElementsByClassName("nvItem")

menu.addEventListener("click", () => {
  navigation.style.setProperty("--childenNumber", navigation.children.length);

  navigation.classList.toggle("active");
  menu.classList.toggle("active");
  console.log("---Active---");
});

Array.from(navItems).forEach(item => {
  item.addEventListener("click", e => {
    navigation.classList.toggle("nonActive");
    menu.classList.toggle("nonActive");
    console.log("---Deactive---");
  });
});
<div id="mn">
  menu
</div>
<ul id="nv">
  <li class="nvItem"><a href="#">Home</a></li>
  <li class="nvItem"><a href="#the-story">Story</a></li>
  <li class="nvItem"><a href="#comic-book">Comic</a></li>
  <li class="nvItem"><a href="#creatures">Creatures</a></li>
  <li class="nvItem"><a href="#the-path">Path</a></li>
  <li class="nvItem"><a href="#artists">Jozef</a></li>
</ul>

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!