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

132
Views
Accordion component does not close when toggled

So I have an accordion component reused from bootstrap accordion component I needed to dynamically add them wrt the response from server. Now if I use the static html from the above link it works and toggles properly. However when I do the below code it does not work:

Wrraper section

 <div class="accordion" id="accordionExample"></div>

JS part:

Here item is coming from the response made from an ajax request.

$(".accordion").append(
                `<div class="accordion-item">
                <h2 class="accordion-header" id=${item._id}>
                    <button
                        class="accordion-button"
                        type="button"
                        data-bs-toggle="collapse"
                        data-bs-target=#collapse${item._id}
                        aria-controls=collapse${item._id}
                        aria-expanded="false"
                    >
                        ${item.productName}
                    </button>
                </h2>
                <div
                    id=collapse${item._id}
                    class="accordion-collapse collapse ${
                        index == 0 ? "show" : ""
                    }"
                    aria-labelledby=${item._id}
                    data-bs-parent="#accordionExample"
                >
                    <div class="accordion-body">
                        ${item.description}
                    </div>
                </div>
            </div>`
            );

When I click on an unexpanded accordion that one expands and the others close. However when I try to close an already expanded accordion item it blinks(in the dom I can see that the class changes from collapsing to collapsed and return back again.) The aria-expanded also keeps remaining to true which is why I assume it does not collapse.

What should I edit to make it work.

about 4 years ago · Santiago Gelvez
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!