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

Local Storage for shopping cart in Javascript

In a previous exercise class with students, we created a simple shopping cart with JS. Today I had another lecture and I added a localStorage feature to this project. In this version of the project, the added items can persist in the cart after a refresh and we added another file named cart.html that shows the added cart items. You can see the source code of this project in the v2.0.0 tag in this repo: JsShoppingCart ...

May 23, 2022 · 2 min · Hamid

Simple Shopping Cart with pure Javascript

I’m a code mentor and private programming tutor. Today, my student and I were working on event listeners in JS and I tried to teach how to make a simple and minimal shopping cart. If you are learning JS, this could be a good exercise. You can find the code here. And you can see a demo here. In this work, we added our products like this: <div class="product"> <h3>Product one</h3> <p>Price: 2000</p> <button data-id="1" data-price="2000">Add to cart</button> </div> We know that in the real world we do not do this manually and we generate them with loops using data that we have retrieved from a database. ...

May 18, 2022 · 2 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