Issue
I'm working on an e-commerce store, it has a cart page where there are quantity increase and decrease buttons for every product added to the cart, I'm trying to reload the page every time someone hits any of these buttons.
So when I have only one product in the cart, the reloading works fine when clicking on the buttons, but when I have more than one product in the cart and I try to increase or decrease the quantity of the second or the third product, it doesn't reload. It seems reloading only works with the first product that has been added, any clue why this is happening? I'm guessing this has something to do with DOM, but how do I fix this?
{% for item in items %}
<div class="quantity">
<p id="quantity-style" class="quantity-style">{{item.quantity}}</p>
<div class="quantity-arrows">
<img data-product="{{item.product.id}}" id="reload_arrow_up" data-action="add" src="{% static 'shop/images/arrow-up.png' %}">
<img data-product="{{item.product.id}}" id="reload_arrow_down" data-action="remove" src="{% static 'shop/images/arrow-down.png' %}">
</div>
</div>
{% endfor %}
$('#reload_arrow_down').click(function() {
location.reload();
});
$('#reload_arrow_up').click(function() {
location.reload();
});
Solution
An id
should only be assigned once. Currently, you are trying to assign the same pair of id
s (reload_arrow_up
and reload_arrow_down
) to every single product. This is probably why the JavaScript functions only work for the first product. Instead, do it as it's explained here, by using class
instead of id
: The class
of several HTML objects can be the same, which is what you need here.
Answered By - David Schultz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.