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

108
Views
Dropdown menu - always showing the last div

there!

I would like to ask you how can I open the correct dropdown with JS. It is working, but no matter which I am clicking it is always showing me the last dropdown div. The piece of code that I have marked should help with this problem, but it doesn't work.

<div class="dropdown-wrapper" data-dropdown>
<a class="dropdown-link" href="#" data-dropdown-button>Makeup</a>
<div class="dropdown-menu"></div>
document.addEventListener("click", e => {
    const isDropDownButton = e.target.matches("[data-dropdown-button]");
    if (!isDropDownButton && e.target.closest("[data-dropdown]") != null) return;
    let currentDropDown;
    if (isDropDownButton) {
        currentDropDown = e.target.closest("[data-dropdown]");
        currentDropDown.classList.toggle("active");
    }

//THIS CODE//

    document.querySelectorAll("[data-dropdown].active").forEach(dropdown => {
        if (dropdown === currentDropDown) return;
        dropdown.classList.remove("active");
    })
})

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In JavaScript you are selecting the element by closest [data-dropdown], therefor add this on the DIV tags.

<div class="dropdown-wrapper" data-dropdown>
    <a class="dropdown-link" href="#" data-dropdown-button>Makeup</a>
    <div class="dropdown-menu" data-dropdown>
        <a class="dropdown-link" href="#" data-dropdown-button>1</a>
        <a class="dropdown-link" href="#" data-dropdown-button>2</a>
        <a class="dropdown-link" href="#" data-dropdown-button>3</a>
    </div>
</div>

your JavaScript works and no need to change it. by clicking the links 1,2,3 it will add a class="active" in the second div.

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!