I am currently working on a webshop, and I'm trying to create a product quantity button. I have three increments and three decrement buttons with the same classes. I'm trying to make them each target their own input field. But what happens to me is that no matter which of the three buttons I click, it only works on the third input field. I am fairly new to javascript and could use any help. Thank you.
My work so far:
Javascript:
var elem = [].slice.call(document.querySelectorAll('input[type="number"]')).pop()
document.querySelectorAll(".product-quantity-button-up").forEach(function(button){
button.addEventListener("click",function(){
elem.value =Number(elem.value) + 1;
})
})
document.querySelectorAll(".product-quantity-button-down").forEach(function(button){
button.addEventListener("click",function(){
elem.value =Number(elem.value) - 1;
if(elem.value<=0){
elem.value=0
}
})
})
html (same code x 3)
<div class="product-quantity-button-container">
<div>
<input type="number" min="1">
</div>
<div class="product-quantity-arrow-container">
<button class="product-quantity-button-up">
<img class="product-quantity-arrow" src="arrow-up.svg">
</button>
<button class="product-quantity-button-down">
<img class="product-quantity-arrow" src="arrow-down.svg">
</button>
</div>
</div>