Skip to content
Advertisement

Update textfield using a selectbox in ruby on rails

I am new to rails and I have a form below where I need to update a text field(car price) using the option selected in the select box (adjust price). drop down needs to have multiple percentage option like 100% , 50% and 25%. when one percentage is selected then it should update the car price in the field with new calculated adjusted price.

JavaScript

I am not sure about the select box that how I can update the car price on the fly ? Thanks in advance.

Advertisement

Answer

Have a look at rails guides, you can use options_for_select combined with select_tag

JavaScript

this will output

JavaScript

Now, to update the price dynamically you’ll probably want to use javascript in your front-end for example by adding an event listener to your select. Then, when selected option is for example 50% you can do calculations directly on the price field by changing it’s value.

JavaScript

or as the question was marked for jQuery you can use .val()

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