Shalafister's
Shalafister's

Reputation: 881

Jquery binding input fields each other

I have 2 input fields and I would like to bind them together without infinite loop.

<%= number_field_tag "owner_discount_percent", @request.discount_owner_percent, class: 'form-control owner-disc' %>

<%= number_field_tag "owner_discount", @request.discount_owner_val, class: 'form-control owner-disc' %>

Lets assume that there is a total value, which is 1000 €.

An owner can give a discount as either percentage or as integer.

If user gives discount as percentage the integer value should calculate automatically to show the discount as integer such as 100€ (1000 * 10%) by updating the integer value field.

But then if user gives discount as integer than it should calculate the percentage and write it to percentage field.

I tried to use on change event gives infinite error. Is there any other way?

Thank you.

Upvotes: 0

Views: 77

Answers (1)

John Meyer
John Meyer

Reputation: 2356

Maybe you could have the action fire on key up/down or focusout within the input fields. On change, as you discovered will create a loop.

https://api.jquery.com/focusout/

Upvotes: 1

Related Questions