Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!