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

174
Vistas
How to display submenus for each element of a menu in a responsive menu in JavaScript

i'm having issue with a responsive menu for screensize < 600px. I want to be able to display a submenu for each element in the menu by clicking on it. Take a look a the code available on the link below and you'll see in the js section, i have a for loop to add an eventListener on each item of the menu but inside the eventListener my counting variable (named i) is always equal to 6 therefore there are only 6 elements in the menu (so from 0 to 5).

var menu = document.getElementById('pull');
var list = document.querySelector('ul.clearfix');
var item = document.querySelectorAll('.item');
var dropdown = document.querySelector(".dropdown");

window.addEventListener("DOMContentLoaded", (event) => {
menu.addEventListener('click', function(){
    list.classList.toggle('active');
});
for(var i = 0 ; i < item.length ;  i++){
  
    item[i].addEventListener('click', function(){
        dropdown.classList.toggle('active');
    });
}
    
window.addEventListener('resize', function(){
    if (window.matchMedia("(min-width: 480px)").matches) {
        if(list.style.display == "none"){
            list.removeAttribute('style');
        }
      }
   });
});

This means i can't use a querySelectorAll on my submenus (class="dropdown") to display them because dropdown[6] does not exist I need to do that in raw js 😭 (and i'm struggling to find an answer to my problem 🥲 ) Here is the codepen link https://codepen.io/raumain/pen/zYzXrwM

about 4 years ago · Juan Pablo Isaza
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