Bootstrap open Accordion after Ajax loaded

Accordion is a nice component of Bootstrap. It uses the Collapse feature of Bootstrap. It works properly under the hood, but sometimes you need to do something different. In my case, I needed to click on the accordion header, then send an Ajax request to get the content of the accordion, and then open it. There was not a straightforward API or documentation for that, and finally I found it. <script> let prevent = true; $("#collapseThree").on("show.bs.collapse", function (e) { if (prevent) { e.preventDefault(); } setTimeout(function () { prevent = false; $("#collapseThree").collapse("show"); prevent = true; }, 1000); }); </script> In this example, I’ve added an event listener on the open state of collapse and I’ve used a setTimeout function to simulate an Ajax request.

June 4, 2022 · 1 min · Hamid

Simple exercise for Javascript Ajax

I have about 4 years of experience working as a private tutor of programming. About five years ago, I published a blog post on my website in Persian language entitled “Private Laravel Tutor” and people started to call me. I always love teaching people and it was a good opportunity. After a while, the number of students increased and today I have two sessions per day on average. Most of my previous students are now working around the world. ...

May 14, 2022 · 2 min · Hamid