Forum: Ruby on Rails how to add onchange javascript event to select field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7ca2a9737729d977d5c7526c33685fb0?d=identicon&s=25 Scott Claerhout (sclaerhout)
on 2006-03-07 19:21
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
C941170ba5ca038b89b8407c83fb23c2?d=identicon&s=25 Berin Loritsch (Guest)
on 2006-03-07 19:30
(Received via mailing list)
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/Help...

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.
7ca2a9737729d977d5c7526c33685fb0?d=identicon&s=25 Scott Claerhout (sclaerhout)
on 2006-03-07 19:56
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 Loritsch 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/Help...
>
> 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.
C941170ba5ca038b89b8407c83fb23c2?d=identicon&s=25 Berin Loritsch (Guest)
on 2006-03-07 20:00
(Received via mailing list)
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')"} )%>
7ca2a9737729d977d5c7526c33685fb0?d=identicon&s=25 Scott Claerhout (sclaerhout)
on 2006-03-07 21:37
Berin,
Thank you, that worked!
Scott
B530c9ece5b1ad6c71030fc7d9a0c7a6?d=identicon&s=25 Rolando V. (rolando_v)
on 2012-07-19 17:34
know if there is a way to know the id or the customer has selected??
D8fd374e9e1811e5b5b8f37b667916bd?d=identicon&s=25 Max (Guest)
on 2012-07-20 15:39
(Received via mailing list)
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();
This topic is locked and can not be replied to.