Adding javascript onchange event on select tag

Hi, I am writing following code for showing the drop down:- <%= select("test", "type", ["test1", "test2"], :onchange=>"alert('Test');")%>

My problem is I have to add onchange event and call a javascript. Can anyone point me what is problem with my syntex?

Thanks, Tushar

I am writing following code for showing the drop down:-

<%= select(“test”, “type”, [“test1”, “test2”],

:onchange=>“alert(‘Test’);”)%>

My problem is I have to add onchange event and call a javascript.

Can anyone point me what is problem with my syntex?

Easy one…

http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select

The first hash is Select options, the second is HTML options. So you’re passing :onchange through as an option to the Rails helper, rather than having it pass through to an HTML attribute. What you want to do is this:

<%= select(“test”, “type”, [“test1”, “test2”], {} {:onchange=>“alert(‘Test’);”} )%>

Note the empty {} after your normal parameter, that’s the empty select options, then your :onchange is in the HTML options hash.

Cheers,

Andy