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

76
Views
Dropdownmenu-Limitation to 1

I just wanted to ask you if it is possible to limit the amount of Dropdownmenus opened at the same time. In the following you can see the code of my dropdownmenu. I want to include a funtion that limits the amount of dropdownmenus opened to 1. So if no menu is opened it should just open normal and if a menu is already open it should close this one and open the selected one.

<script>

var dropdown = document.getElementsByClassName("dropdownbtn");
var i;

for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
    } else {
      dropdownContent.style.display = "block";
    }
  });
}

</script>

Thanks in advance.

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

0

Your ".dropdownbtn" in place makes it more complicated. What do you think of customization?

<nav>
  <ul class="my-nav">
    <li>
      <details class="dropdownbtn">
        <summary>First</summary>
        <ul>
          <li><a href="#">1</a></li>
          <li><a href="#">2</a></li>
        </ul>
      </details>
    </li>
    <li>
      <details class="dropdownbtn">
        <summary>Second</summary>
        <ul>
          <li><a href="#">3</a></li>
          <li><a href="#">4</a></li>
        </ul>
      </details>
    </li>
  </ul>
</nav>

And than your logic:

var nav = document.querySelector('.my-nav');
nav.addEventListener('toggle', (event) => {
  
  // Only run if the dropdown is open
  if(!event.target.open) {
    return;
  }

  // Get all other open dropdowns and close them
  var dropdowns = nav.querySelectorAll('.dropdownbtn[open]');
  Array.prototype.forEach.call(dropdowns, (dropdown) => {
    if(dropdown === event.target) { 
      return;
    }
    dropdown.removeAttribute('open');
  });

}, true);

I have this solution for you on codepen.

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!