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

212
Vistas
javascript queryselectorall nodelist select all accept this

I can not seem to find how to get this done proper.

const menu = document.querySelectorAll('.menu-item');

menu.forEach(item=>{
   item.addEventListener('click', ()=>{
      // now I need to select all menu items accept 'this' to remove 'is-active' class.
      document.querySelector(".menu-link").classList.remove("is-active"); // not working
      !this.classList.remove("is-active"); // not working

      this.classList.add("is-active"); // add active class to menu item
   });
});

I know I could again loop trough all items then remove for each item the class and last add for 'this', but I think there should be a way to select all but this.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Iterate over all menu items again - you're already doing forEach to add the listeners, so just do that again inside the listener. After that, reference the item (the item being iterated over) to add the class. (Using this won't work because you're using an arrow function.)

const menuItems = document.querySelectorAll('.menu-item');
menuItems.forEach(item => {
    item.addEventListener('click', () => {
        menuItems.forEach(item => {
            item.classList.remove("is-active");
        });
        item.classList.add("is-active");
    });
});

Or save the last active one in a variable

let lastActive;
const menuItems = document.querySelectorAll('.menu-item');
menuItems.forEach(item => {
    item.addEventListener('click', () => {
        lastActive?.classList.remove("is-active");
        item.classList.add("is-active");
        lastActive = item;
    });
});

If you really want to go the comparison route, then

const menuItems = document.querySelectorAll('.menu-item');
menuItems.forEach(item => {
    item.addEventListener('click', () => {
        menuItems.forEach(innerItem => {
            if (innerItem !== item) {
                innerItem.classList.remove("is-active");
            }
        });
        item.classList.add("is-active");
    });
});

or if you must use this, don't use an arrow function.

const menuItems = document.querySelectorAll('.menu-item');
menuItems.forEach(item => {
    item.addEventListener('click', function() {
        menuItems.forEach(innerItem => {
            if (innerItem !== this) {
                innerItem.classList.remove("is-active");
            }
        });
        this.classList.add("is-active");
    });
});
about 4 years ago · Santiago Gelvez 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