Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda