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

239
Visualizações
How to select all elements and remove a CSS class

I have the following JS function which on click is making the chosen element to be selected(is changing the background color) and should make all other elements to remove this class. However, for some reason onClick the "menu-category-hover" class is not removed.

function GetData(allMenuItems, theCategoryId) {
    
    // Remove selected class on other Elements
    allMenuEl = document.querySelectorAll('menu-category');
    for (var i = 0; i < allMenuEl; i++) {
        allMenuEl[i].classList.remove("menu-category-hover");
    }

    .....

    // On element Click
    var hoverEl = document.getElementById("menu_cat_" + theCategoryId);
    hoverEl.classList.add("menu-category-hover");
}

<div class="menu-category menu-category-hover" onclick="GetData(, 13)" id="menu_cat_13">
     <img src="/images/85783_burger.png" class="menu-home-image-img">
     <p class="menu-home-name">Burgers</p>
<div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Check this

function GetData(allMenuItems, theCategoryId) {
    
    // Remove selected class on other Elements 
    allMenuEl = document.querySelectorAll('.menu-category'); //<--- class indicator `.`
    for (var i = 0; i < allMenuEl.length; i++) { //<-- not added allMenuEl.length
        allMenuEl[i].classList.remove("menu-category-hover");
    }

    .....

    // On element Click
    var hoverEl = document.getElementById("menu_cat_" + theCategoryId);
    hoverEl.classList.add("menu-category-hover");
}

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