How to fix the increase and decrease value of my shopping card? (PHP & Javascript)
When I add a product in my shopping card and I decrease or increase the value, everything works. But when I want to add another product in my shopping card and I want to decrease or increase the value of that product, it change the value of the first one. I don't know where the problem is.
function increaseValue() {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
}
function decreaseValue() {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value < 1 ? value = 1 : '';
value--;
document.getElementById('number').value = value;
}
// image with explanations
https://i.stack.imgur.com/hE1k2.png
Your javascript uses ID to target the element to be updated. You should not repeat an ID on an html page so you should realise this is wrong when you put it in a foreach loop
each iteration of the loop you need to vary the field names.
@TRAY2 is correct, but you also need to change the name and ID of the quantity field