I am trying to use some javascript inside a form to get the value from a select dropdown box, and use it another part of the form. I cannot figure out the syntax.
first the javascript function (I placed this at the end of my view):
<script type="text/javascript">
function return_event_id()
{
var event_id = document.getElementById("event_id");
return event_id.value;
}
</script>
Inside the view, I have the following:
<%= select("discount_code", :event_id, events_ids_titles_hash, { :include_blank => true }) %>
If I add the following statement:
return_event_id
Then the dropdown item I selected is displayed on the form (although I also see the word return_event_id as well).
If I do something like this:
<% selected_event_id = return_event_id %>
I get the following error message: undefined local variable or method `return_event_id'
What I'm looking for is to be able to store the event_id value in a variable. What's the proper syntax?
Additional details and comments on answer:
1: Yes, Ruby code = server, javascript code = client
2: Putting the code in something like application.js would move it out of the view.erb module, but as far as I know, it does not change the outcome.
I understand that <% selected_event_id = return_event_id %> will not work, but I do need to get to a point where I can somehow pass the output from return_event_id back to the server, and then capture it in a Ruby variable, OR, if there is a way to use the javascript function output to set another element in the form.