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

221
Visualizações
javascript toggle not working when the remove exist

In the code below I remove all the active classes from the div box then i add the active class for the current one using the toggle

all of that work fine

but the problem is when I click on the same box, for example, box1 it doesn't remove the active class I should click on the second one to remove it and in my code, I use the toggle, not the add.

I want when I click on a box the box open and when I click again on it it should be close again and if I click on another box after the first box should be close

what's wrong with my code?

let faqs = document.querySelectorAll(".allsector .box");

faqs.forEach((faq) => {
    faq.addEventListener("click", function (e) {
        faqs.forEach((ele) => {
            ele.classList.remove("active");
        });
        faq.classList.toggle("active");
    });
});
.allsector .box .content {
    max-height: 0;
    visibility: hidden;
    opacity: 0;
    transition: all .5s ease-in-out;
}

.allsector .box.active .content {
    max-height: 300px;
    visibility: visible;
    opacity: 1;
    }
    
.allsector .box.active .fa-angle-down {
    transform: rotate(180deg);
}
<script src="https://kit.fontawesome.com/9e5ba2e3f5.js" crossorigin="anonymous"></script>
<div class="allsector">
<div class="box">
<div class="title">title 1 <i class="fas fa-angle-down"></i></div>
<div class="content">content 1</div>
</div>
<div class="box">
<div class="title">title 2 <i class="fas fa-angle-down"></i></div>
<div class="content">content 2</div>
</div>
<div class="box">
<div class="title">title 3 <i class="fas fa-angle-down"></i></div>
<div class="content">content 3</div>
</div>
</div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Remove forEach, use this:

let faqs = document.querySelectorAll(".allsector .box");

    faqs.forEach((faq) => {
        faq.addEventListener("click", function (e) {
            faq.classList.toggle("active");
        });
    });
about 4 years ago · Juan Pablo Isaza Relatório

0

Remove forEach from addEventListener.

let faqs = document.querySelectorAll(".allsector .box");

faqs.forEach((faq) => {
 faq.addEventListener("click", function (e) {
    faq.classList.toggle("active");
 });
});
about 4 years ago · Juan Pablo Isaza Relatório

0

If classList.toggle() doesn't work, there is classList.add(), classList.remove() too. In the function classList.toggle() was keep by classList.remove(). I just simplified with if conditions in one forEach loop.

let faqs = document.querySelectorAll(".allsector .box");

faqs.forEach((faq, idFaq) => {
    faq.addEventListener("click", function (e) {
        faqs.forEach((ele, idEle) => {
            idFaq === idEle && faqs[idEle].classList.contains("active") === false ?             
            faqs[idEle].classList.add("active") :
            faqs[idEle].classList.remove("active");            
        });        
    });
});
.allsector .box .content {
    max-height: 0;
    visibility: hidden;
    opacity: 0;
    transition: all .5s ease-in-out;
}

.allsector .box.active .content {
    max-height: 300px;
    visibility: visible;
    opacity: 1;
    }
    
.allsector .box.active .fa-angle-down {
    transform: rotate(180deg);
}
<script src="https://kit.fontawesome.com/9e5ba2e3f5.js" crossorigin="anonymous"></script>
<div class="allsector">
<div class="box">
<div class="title">title 1 <i class="fas fa-angle-down"></i></div>
<div class="content">content 1</div>
</div>
<div class="box">
<div class="title">title 2 <i class="fas fa-angle-down"></i></div>
<div class="content">content 2</div>
</div>
<div class="box">
<div class="title">title 3 <i class="fas fa-angle-down"></i></div>
<div class="content">content 3</div>
</div>
</div>

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