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

170
Vistas
How to close a dynamically created dropdown list?

I have a .hidden class in my CSS file and I toggle the class when the user clicks an ellipsis button which reveals a dropdown list. The list should disappear when the user clicks anywhere on the window. This is the HTML created dynamically:

<div class="dropdown">
         <span class="options"><i class="fa-solid fa-ellipsis-vertical"></i></span>
          <div id="myDropdown" class="dropdown-content hidden">
            <a href="#"><i class="fa-solid fa-pen"></i> Edit workout</a>
            <a href="#"><i class="fa-solid fa-circle-minus"></i> Delete workout</a>
            <a href="#"><i class="fa-solid fa-trash"></i> Clear all workouts</a>
          </div>
</div>

Below is the method that toggles the .hidden class which works fine but will become unable to remove the .hidden after I call the method which should add the .hidden class back

containerWorkouts.addEventListener('click', function (e) {
      const items = containerWorkouts.querySelector('.dropdown-content');
      if (e.target.classList.contains('fa-ellipsis-vertical')) {
        items.classList.remove('hidden');
      }
      
    });

Below is the method that adds the .hidden class back to hide the list

sidebarContainer.addEventListener('click', function(e){
      const dropdown = sidebarContainer.querySelector('.dropdown');
      const items = containerWorkouts.querySelector('.dropdown-content');
  
      if (!dropdown.classList.contains(e.target)) {
        if (!items.classList.contains('hidden')) {
          items.classList.add('hidden');
        }
      }
    }
about 4 years ago · Santiago Gelvez
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