Skip to content
Advertisement

jQuery coloring table row if quantity is same

I have a table with order items, with a quantity input box and a field with the quantity that has been ordered.

If the quantity input box has the same value as the quantity that has been ordered, I want it to color green.

Now I have it working a bit, but my knowledge only goes so far. Somehow it only works if I click the items in order, so I have to click the input box on row one.. and when it matches the ordered quantity it works.

But if I click the second row-item first.. it won’t work.

This is the Jquery. How do I edit this so it doesn’t matter which row I use first?

JavaScript
JavaScript

Advertisement

Answer

First define a function for set color that it accept closest row of +/- buttons and quantity input as argument

and use that in events

JavaScript
JavaScript

I do not know how much control you have over the your HTML structure, because then we could have a cleaner HTML and js code.

for example:

you have many rows with same id that It is not common,

OR

you used this code var $target = $(this).next('input[name=' + $(this).attr('field') + ']'); for get quantity input that I think it’s not necessary. you can use quantity instead of + $(this).attr('field') + And examples like these.

I say again Since I do not know the complete structure of the page, I guess you may have reasons and limitations and I tried not to change too much but you can do it much easier

good luck

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