I am having a carousel with products and I want to show a loading Spinner when each and every Add to Cart button is clicked. But the code I have only shows a spinner on the first button clicked. Others wont show afterwards. Please Help
The Carousel
<div class="carousel-multiple owl-carousel owl-theme">
<div class='item'>
<div class="card product-card">
<div class="card-body">
<img src="assets/img/sample/photo/product1.jpg" class="image" alt="product image">
<h2 class="title">Apple</h2>
<p class="text">1 kg</p>
<div class="price">$ 1.50</div>
<a href="#" class="btn btn-sm btn-primary btn-block" id='spinner-btn'>ADD TO CART</a>
</div>
</div>
</div>
<div class='item'>
<div class="card product-card">
<div class="card-body">
<img src="assets/img/sample/photo/product1.jpg" class="image" alt="product image">
<h2 class="title">Apple</h2>
<p class="text">1 kg</p>
<div class="price">$ 1.50</div>
<a href="#" class="btn btn-sm btn-primary btn-block" id='spinner-btn'>ADD TO CART</a>
</div>
</div>
</div>
<div class='item'>
<div class="card product-card">
<div class="card-body">
<img src="assets/img/sample/photo/product1.jpg" class="image" alt="product image">
<h2 class="title">Apple</h2>
<p class="text">1 kg</p>
<div class="price">$ 1.50</div>
<a href="#" class="btn btn-sm btn-primary btn-block" id='spinner-btn'>ADD TO CART</a>
</div>
</div>
</div>
</div>
The Javascript Code for showing spinner on button click
$(document).ready(function () {
$("#spinner-btn").click(function () {
// disable button
$(this).prop("disabled", false);
// add spinner to button
$(this).html(
'<span class="spinner-border spinner-border-sm mr-05" role="status" aria-hidden="true"></span> Loading'
);
});
});
2
Answers
ID attribut must be unique !
If you want to target more than one element, you need to use a class.
https://careerkarma.com/blog/css-class-vs-id-2/
You are using the same
ID
on all of them, this will cause you problems because an ID element must be unique and used in 1 place only.Try giving them all a shared class, or change the
a
elements to abutton
element, and give it a certain type for example: