Skip to content
Advertisement

JS doesn’t change the value of input

I’m writing cart-box that will change the quantity of products in cart. It works only if I have one box (one product) in cart, but when I have more products in cart it changes the value of the first input only.

This is my html code (earlier in the code I’ve got loop for my products):

JavaScript

And this is JS code:

JavaScript

I know that document.querySelector("input").value = count changes the first input only, because it’s first on the list, but input['value'] = count doesn’t change anything, parent.closest("input").value = count either.

Advertisement

Answer

Make sure you use valid HTML, otherwise results are not guaranteed.

Next let’s remove duplication and just use the one event listener for both buttons, changing the value added based on the presence of the plus class.

Finally, if you’re using jQuery, stick to using jQuery methodology. Also, you are doing nothing here with jQuery that couldn’t be done with simple, native, javascript.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement