Skip to content
Advertisement

How to make 3 buttons(same class ,increment on click) that each target individual input fields(number) to work, and not just the last input field?

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

html (same code x 3)

JavaScript

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
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement