How to add onchange javascript event to select field


#1

I would like to add javascript ‘onChange’ event handling for a select
field, such as
<%= collection_select(“job”, “client_id” , @clients, “id”, “name”) %>
with onChange=“xxx”

Anyone know how to make this work?
Thank you,
Scott


#2

Scott Claerhout wrote:

I would like to add javascript ‘onChange’ event handling for a select
field, such as
<%= collection_select(“job”, “client_id” , @clients, “id”, “name”) %>
with onChange=“xxx”

Anyone know how to make this work?

http://rubyonrails.org/api/classes/ActionView/Helpers/FormOptionsHelper.html#M000352

The full API is:

collection_select(object, method, collection, value_method,
text_method, options = {}, html_options = {})

Just add :onchange => “xxx” to the html_options and you are set.


#3

Berin,
Thank you for responding. I cannot get the syntax correct. Would it be
something like:
<%= collection_select(“job”, “client_id” , @clients, “id”, “name”,
html_options = {:onchange => “alert(‘hello’)”} )%>

or

<%= collection_select(“job”, “client_id” , @clients, “id”, “name”,
{:onchange => “alert(‘hello’)”} )%>

Berin L. wrote:

Scott Claerhout wrote:

I would like to add javascript ‘onChange’ event handling for a select
field, such as
<%= collection_select(“job”, “client_id” , @clients, “id”, “name”) %>
with onChange=“xxx”

Anyone know how to make this work?

http://rubyonrails.org/api/classes/ActionView/Helpers/FormOptionsHelper.html#M000352

The full API is:

collection_select(object, method, collection, value_method,
text_method, options = {}, html_options = {})

Just add :onchange => “xxx” to the html_options and you are set.


#4

Berin,
Thank you, that worked!
Scott


#5

know if there is a way to know the id or the customer has selected??


#6

this will give you the selected text if you want it. you can probably
figure out what you need from this example.

var grain = $(‘select#id_of_my_element option:selected’).text();


#7

Scott Claerhout wrote:

You have to get past the “options” parameter before you can use
“html_options”

<%= collection_select(“job”, “client_id” , @clients, “id”, “name”, {},
{:onchange => “alert(‘hello’)”} )%>