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

180
Visualizações
How equality works in javascript

I am making a mobile menu. I want to change class in . The first step works and it removes 'fa-bars' class and adds 'fa-times'. But the second step does not work.

<div class="menuToggle">
       <i id='test' class="fas fa-bars fa-2x"></i>
    </div>     

        let menuActive = document.querySelectorAll('.menuToggle i')
        var element = document.getElementById('test');

        if(element.classList.contains('fa-bars')){
            function Activemenu(){
                menuActive.forEach((item)  =>
                item.classList.remove('fa-bars'));
                this.classList.add('fa-times');
            } 
        }else if(element.classList.contains('fa-bars')){
            function Activemenu(){
                menuActive.forEach((item)  =>
                item.classList.remove('fa-times'));
                this.classList.add('fa-bars');
            }  
        }
        menuActive.forEach((item) =>
            item.addEventListener('click',Activemenu));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are a couple of problems there:

  1. You're using function declarations within if/else blocks. Those are standardized now in certain limited situations (which your code doesn't match), but it's complicated enough that you really just want to avoid using function declarations in blocks. Use function expressions assigned to variables instead.

  2. You're using an arrow function for the event handler, but expecting this to be set by how the function is called. Arrow functions don't work that way.

  3. You're checking for fa-bars in both classList.contains checks. You probably wanted fa-times in the second one.

  4. You're only binding the first event handler to that element. Nothing updates the handler when the condition changes.

And just as a side note, I'd suggest using for-of loops over forEach callbacks:

const menuActive = document.querySelectorAll(".menuToggle i");
const element = document.getElementById("test");
function activeMenuClickHandler() {
    if (element.classList.contains("fa-bars")) {
        for (const item of menuActive) {
            item.classList.remove("fa-bars"));
        }
        this.classList.add("fa-times");
    } else if (element.classList.contains("fa-times")) {
        for (const item of menuActive) {
            item.classList.remove("fa-times"));
        )
        this.classList.add("fa-bars");
    }
}
for (const item of menuActive) {
    item.addEventListener("click", activeMenuClickHandler);
}

(Or similar, you may have to do some further tweaking.)


If this code only ever toggles fa-bars/fa-times, it may well be simpler to use the toggle function of classList:

const menuActive = document.querySelectorAll(".menuToggle i");
const element = document.getElementById("test");
function activeMenuClickHandler() {
    this.classList.toggle("fa-bars fa-times");
    for (const item of menuActive) {
        item.classList.toggle("fa-bars fa-times");
    }
}
for (const item of menuActive) {
    item.addEventListener("click", activeMenuClickHandler);
}
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