# rails and javascript

**URL:** <https://discuss.rubyonrails.org/t/rails-and-javascript/63045>\
**Category:** rubyonrails-talk\
**Created:** [February 28, 2012, 9:50pm UTC](https://discuss.rubyonrails.org/t/rails-and-javascript/63045 "2012-02-28T21:50:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dev\_Guy](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dev\_Guy](https://discuss.rubyonrails.org/u/Dev_Guy)\
**Post date:** [February 28, 2012, 9:50pm UTC](https://discuss.rubyonrails.org/t/rails-and-javascript/63045/1 "2012-02-28T21:50:08Z")

</div>

Hi,

I want to add a dropdown selection box that when an item is selected a javascript function gets called, is there an easy way to do this with Rails?

Thanks!

---

<div class="post-metadata">

**Author:** ![Dave\_Aronson2](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Dave\_Aronson2](https://discuss.rubyonrails.org/u/Dave_Aronson2)\
**Post date:** [February 29, 2012, 8:23pm UTC](https://discuss.rubyonrails.org/t/rails-and-javascript/63045/2 "2012-02-29T20:23:06Z")

</div>

Sure, though it's not Rails-specific; just use the onchange attribute. See:

[onchange Event](http://www.w3schools.com/jsref/event_onchange.asp)

for an intro to the concept.

-Dave

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [March 1, 2012, 4:21am UTC](https://discuss.rubyonrails.org/t/rails-and-javascript/63045/3 "2012-03-01T04:21:34Z")

</div>

There are a few ways to do this. If you are looking for an ajax response you can do the following:

(this is an example with a new action)

\<%= form\_for Model.new, :remote =\> true do |f| %\>

\<div\>Your Label\</div\> \<div\> &nbsp;&nbsp;\<%= f.collection\_select(:some\_id, Model.all, :id, :name, :class =\> 'yourclass' %\> \</div\>

\<% end %\>

Then in your javascript do something similar to:

$('select#model\_some\_id').bind({ &nbsp;&nbsp;click:function () { &nbsp;&nbsp;&nbsp;&nbsp;// do something when it's clicked &nbsp;&nbsp;}, &nbsp;&nbsp;keydown:function () { &nbsp;&nbsp;&nbsp;&nbsp;// do something when a key is pressed down in the select &nbsp;&nbsp;}, &nbsp;&nbsp;keyup:function () { &nbsp;&nbsp;&nbsp;&nbsp;// do something when a key is pressed up in the select &nbsp;&nbsp;} });

This handles most of the browser events you'll find in Mozilla, IE, Safari, etc. as far as handling events. Some change events are not noticed by Mozilla (for instance if someone is using drop down arrows while in a select field).

There are quite a few ajax tutorials out there.

---

<div class="post-metadata">

**Author:** ![Dev\_Guy](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dev\_Guy](https://discuss.rubyonrails.org/u/Dev_Guy)\
**Post date:** [March 2, 2012, 5:57pm UTC](https://discuss.rubyonrails.org/t/rails-and-javascript/63045/4 "2012-03-02T17:57:49Z")

</div>

Hi thanks,

this is exactly what I was looking for, a solution using jQuery to bind a function. Still green on ajax, so your example was very useful and also the link provided above for a javescript refresher on events.
