Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
How to make JS code accept multiple similar IDs

I have the JS code below that I use with the form after it. However, it only calls the first dropdown and ignores the rest. The dropdown are generated by Hugo (SSG) and I all share the same ID/class in the menu. I need help to make the code work for multiple dropdown menus.


 document.getElementById("dropbtn").addEventListener('click', function() { 
  document.getElementById("sub-menu").classList.toggle("show");
  var x = document.getElementById("dropbtn").getAttribute("aria-expanded"); 
  if (x == "true") 
  {
  x = "false"
  } else {
  x = "true"
  }
  document.getElementById("dropbtn").setAttribute("aria-expanded", x);
});
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("sub-menu");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
<nav>
           <ul>
             <li class="dropdown" id="dropdown">
              <a id="dropbtn" class="dropbtn" href="#" aria-expanded="false">Toggle<span class="drop-icon" for="toggle">▾</span></a>
              <ul id="sub-menu" class="sub-menu">
                <li>
                  <a href="/a/">A</a>
                </li>
                <li>
                  <a href="/b/">B</a>
                </li>
                <li>
                  <a href="/c/" >C</a>
                </li>
              </ul>
             </li>
             <li class="dropdown" id="dropdown">
              <a id="dropbtn" class="dropbtn" href="#" aria-expanded="false">Dropdown <span class="drop-icon" for="dropdown">▾</span></a>
              <ul id="sub-menu" class="sub-menu">
                <li>
                  <a href="/d/">D</a>
                </li>
                <li>
                  <a href="/e/">E</a>
                </li>
                <li>
                  <a href="/f/">F</a>
                </li>
              </ul>
             </li>
           </ul>
        </nav>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It's not reccomended to use multiple id's with the same name. Here's my solution for your case.

<nav>
  <ul>
    <li class="dropdown">
      <a class="dropbtn" aria-expanded="false" href="#">Toggle<span class="drop-icon">▾</span></a>
      <ul class="sub-menu">
        <li>
          <a href="/a/">A</a>
        </li>
        <li>
          <a href="/b/">B</a>
        </li>
        <li>
          <a href="/c/">C</a>
        </li>
      </ul>
    </li>
    <li class="dropdown">
      <a class="dropbtn" aria-expanded="false" href="#">Dropdown <span class="drop-icon">▾</span></a>
      <ul class="sub-menu">
        <li>
          <a href="/d/">D</a>
        </li>
        <li>
          <a href="/e/">E</a>
        </li>
        <li>
          <a href="/f/">F</a>
        </li>
      </ul>
    </li>
  </ul>
</nav>

<script>
  let dropbtns = document.getElementsByClassName('dropbtn');
  for (let dropbtn of dropbtns) {
    dropbtn.onclick = () => {
      dropbtn.nextElementSibling.classList.toggle('show');
      let expanded = dropbtn.getAttribute('aria-expanded');
      dropbtn.setAttribute('aria-expanded', expanded == 'true' ? 'false' : 'true');
    };
  }
  // Close the dropdown menu if the user clicks outside of it
  window.onclick = function (event) {
    if (!event.target.matches('.dropbtn')) {
      let dropbtns = document.getElementsByClassName('dropbtn');
      for (let dropbtn of dropbtns) {
        dropbtn.nextElementSibling.classList.remove('show');
        dropbtn.setAttribute('aria-expanded', 'false');
      }
    }
  };
</script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

So, first of all. There can only be one unique ID per page, so your HTML is now invalid. You will have to have different IDs on your HTML elements (if you still need them at all). Below JS hopefully should work.

const dropdownTriggers = document.querySelectorAll('.dropbtn');

[...dropdownTriggers].forEach((trigger) => {
  trigger.addEventListener('click', (e) => {
    const triggerClicked = e.target;
    const dropdownMenu = triggerClicked.nextElementSibling;
    const isDropdownShown = Boolean(triggerClicked.getAttribute('aria-expanded'));
    dropdownMenu.classList.toggle('show', !isDropdownShown);
    triggerClicked.setAttribute('aria-expanded', isDropdownShown);
  })
})

document.addEventListener('click', (e) => {
  if (!event.target.matches('.dropbtn')) {
    [...dropdownTriggers].forEach((trigger) => {
      const dropdownMenu = trigger.nextElementSibling;
      dropdownMenu.classList.toggle('show', false);
      trigger.setAttribute('aria-expanded', false);
    })
  }
});

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda