Rails: how to get value from another field when executing an onchange remote function in textfield

StackOverflow https://stackoverflow.com/questions/1131694

Question

I want to create a form for making a reservation for borrowed items. A reservation consists of pick up time, return time and items the reserver wants to borrow and their amounts. For example, I might want to reserve three plates, three knives and three forks for the rest of the week.

In the form, I want to do an AJAX validation that checks whether there is enough items available. Next to each item I have a text box with which the reserver inputs the item amount. After the input, I want to do an onchange call that checks if the amount of items is available for the given dates. Thus, I need to pass the remote function called in the onchange following parameters: item id, item amount (value of the current textfield) and pick up time and return time which are both given in datetime_select fields above. This is my code:

<% with = "'amount='+value+'&item=#{item.id.to_s}&pick_up_time=#{@reservation.pick_up_time.to_s}&return_time=#{@reservation.return_time.to_s}'" %>
<%= text_field_tag "reservation[#{prefix}reserved_items][#{item.id}]", get_amount_value(item, @reservation), :size => 3, :onchange => "#{remote_function(:url => { :controller => :items, :action => :availability }, :with => with, :update => availability_url) }" %> 

Obviously, this does not work since @reservation.return_time and @reservation.pick_up_time are not yet set because the form is not yet sent. My question is: how do I get those values? I believe that it should be done via a javascript call, but I didn't manage to insert a javascript call in the "with" variable or at least didn't get it to work. Does anybody have any idea on what I should do?

Was it helpful?

Solution

use prototype selectors $(#reservations_pick_up_time).value

the :with attribute is just a place for you to write JS code that it will display inline

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top