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:
JavaScript
x
19
19
1
var elem = [].slice.call(document.querySelectorAll('input[type="number"]')).pop()
2
3
4
document.querySelectorAll(".product-quantity-button-up").forEach(function(button){
5
button.addEventListener("click",function(){
6
7
elem.value =Number(elem.value) + 1;
8
})
9
})
10
11
document.querySelectorAll(".product-quantity-button-down").forEach(function(button){
12
button.addEventListener("click",function(){
13
elem.value =Number(elem.value) - 1;
14
if(elem.value<=0){
15
elem.value=0
16
}
17
})
18
})
19
html (same code x 3)
JavaScript
1
14
14
1
<div class="product-quantity-button-container">
2
<div>
3
<input type="number" min="1">
4
</div>
5
<div class="product-quantity-arrow-container">
6
<button class="product-quantity-button-up">
7
<img class="product-quantity-arrow" src="arrow-up.svg">
8
</button>
9
<button class="product-quantity-button-down">
10
<img class="product-quantity-arrow" src="arrow-down.svg">
11
</button>
12
</div>
13
</div>
14
Advertisement
Answer
I changed the classes to something less verbose and removed unnecessary <div>
s. The original layout of OP will still function properly with the example, just be mindful of the classes — they must be changed either in OP or in the example if keep the original HTML.
Details are commented in example
JavaScript
1
30
30
1
/**
2
Collect all <button> into a NodeList
3
On each <button> register the "click" event
4
Event handler incDec() is called
5
*/
6
document.querySelectorAll('button').forEach(btn => btn.onclick = incDec);
7
8
/**
9
Event handler passes the Event object by default
10
Find the closest <section> then find and reference <input>
11
Reference <input> value and coherce it into a real number
12
If the clicked <button> has the class ".up"...
13
...increment >val<...
14
...otherwise decrement >val<
15
If >val< is less than 0...
16
...assign it as 0...
17
...otherwise keep it's value
18
Assign the value of <input> as >val<
19
*/
20
function incDec(e) {
21
const num = this.closest(".quantity").querySelector("input");
22
let val = +num.value;
23
if (this.matches(".up")) {
24
val++;
25
} else {
26
val--;
27
}
28
val = val < 0 ? 0 : val;
29
num.value = val;
30
}
JavaScript
1
29
29
1
html {
2
font: 300 2ch/1.2 Consolas
3
}
4
5
input,
6
button {
7
font: inherit
8
}
9
10
input {
11
display: block;
12
width: 77px;
13
border: 0;
14
outline: 0;
15
font-size: 1.5rem;
16
text-align: center;
17
}
18
19
button {
20
border: 0;
21
padding: 5px 3px 0;
22
background: transparent;
23
cursor: pointer
24
}
25
26
button:hover,
27
button:active {
28
background: rgba(193, 211, 233, 0.6);
29
}
JavaScript
1
29
29
1
<section class="quantity">
2
<input value="0" readonly>
3
<button class="up">
4
<img src="https://upload.wikimedia.org/wikipedia/commons/7/76/Arrow_%2880554%29_-_The_Noun_Project.svg" width="32" height="32">
5
</button>
6
<button class="down">
7
<img src="https://upload.wikimedia.org/wikipedia/commons/6/63/Arrow_%2880557%29_-_The_Noun_Project.svg" width="32" height="32">
8
</button>
9
</section>
10
11
<section class="quantity">
12
<input value="0" readonly>
13
<button class="up">
14
<img src="https://upload.wikimedia.org/wikipedia/commons/7/76/Arrow_%2880554%29_-_The_Noun_Project.svg" width="32" height="32">
15
</button>
16
<button class="down">
17
<img src="https://upload.wikimedia.org/wikipedia/commons/6/63/Arrow_%2880557%29_-_The_Noun_Project.svg" width="32" height="32">
18
</button>
19
</section>
20
21
<section class="quantity">
22
<input value="0" readonly>
23
<button class="up">
24
<img src="https://upload.wikimedia.org/wikipedia/commons/7/76/Arrow_%2880554%29_-_The_Noun_Project.svg" width="32" height="32">
25
</button>
26
<button class="down">
27
<img src="https://upload.wikimedia.org/wikipedia/commons/6/63/Arrow_%2880557%29_-_The_Noun_Project.svg" width="32" height="32">
28
</button>
29
</section>