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

70
Views
Eventlistener - mouseleave - dropdown menu

Trying to figure out how to use the mouseleave event when the mouse leaves all the elements in a dropdown menu. It currently only works when the mouse leaves the submenu, but not the About element as well as the submenu elements. Driving me crazy and been trying to figure it out for HOURS.

<ul class="navigation__list-menu">
        <li class="navigation__list-menu-about">
          <a href="/about" class="now">About</a>
        </li>
        <ul id="nav" class="navigation__list-menu-about-options now">
          <li><a href="/bio">my bio</a></li>
          <li><a href="/cv">curriculum vitae</a></li>
        </ul>

<script>
const navOptions = document.querySelector(".navigation__list-menu-about");

const navOptionMenu = document.querySelector(
  ".navigation__list-menu-about-options"
);


navOptions.addEventListener("mouseenter", (e) => {
  navOptionMenu.classList.add("active");
});

navOptions.addEventListener("mouseleave", (e) => {
  navOptionMenu.classList.remove("active");
});

</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Actually you are only manipulating the sub items. You need to add/remove the class to the about item too. Please have a look at the code snippet below

const navOptions = document.querySelector(".navigation__list-menu-about");

const navOptionMenu = document.querySelector(
  ".navigation__list-menu-about-options"
);


navOptions.addEventListener("mouseenter", (e) => {
  navOptions.classList.add("active");
});

navOptions.addEventListener("mouseleave", (e) => {
  navOptions.classList.remove("active");
});

navOptionMenu.addEventListener("mouseenter", (e) => {
  navOptionMenu.classList.add("active");
});

navOptionMenu.addEventListener("mouseleave", (e) => {
  navOptionMenu.classList.remove("active");
});
<style>
  .active,
  .active a {
    color: red;
  }
</style>
<ul class="navigation__list-menu">
  <li class="navigation__list-menu-about">
    <a href="/about" class="now">About</a>
  </li>
  <ul id="nav" class="navigation__list-menu-about-options now">
    <li><a href="/bio">my bio</a></li>
    <li><a href="/cv">curriculum vitae</a></li>
  </ul>
</ul>

about 4 years ago · Juan Pablo Isaza Report

0

Event bubbling will always pass the event from child tag to parent tag unless there is a JavaScript code to abort it.

Which means by listening to the parent navigation__list-menu you can get all events from its child's too. based of the event.target you can understand where event came from.

If you can't get the event at parent then there might be:

  • Some code that does event.stopPropogation() from child node.
  • your HTML code have Structural issues
about 4 years ago · Juan Pablo Isaza 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!