Sean McCollum
Sean McCollum

Reputation: 5

How to get the value of a select_tag item while still on a form in rails

Here's my select_tag item:

<%= select_tag :room_id, options_from_collection_for_select(Room.all, "id", "to_s") %>

Question: How can I get the selected value of a select_tag object on my form - while still on the form? (not using params[:room_id] inside the controller after it has been submitted)

In other words, I know I can use params[:room_id] in the controller once I've submitted the form. All of my searches about this question seem to tell me to just use params[:room_id]. What I need to do is simply to get the value of the select_tag item while still on the form and use it.

for example: <%= local_var = (value of select_tag) %>

Thanks,

Upvotes: 0

Views: 41

Answers (1)

kjmagic13
kjmagic13

Reputation: 1318

You should be using a client side scripting language such as JavaScript or the jQuery library as shown below:

$( "#id_of_select_element" ).val();

Upvotes: 1

Related Questions