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

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

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 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