# How can I do an onChange event with Rails3 and jQuery?

**URL:** <https://www.ruby-forum.com/t/how-can-i-do-an-onchange-event-with-rails3-and-jquery/194001>\
**Category:** Rails\
**Created:** [August 27, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/how-can-i-do-an-onchange-event-with-rails3-and-jquery/194001 "2010-08-27T17:38:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 27, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/how-can-i-do-an-onchange-event-with-rails3-and-jquery/194001/1 "2010-08-27T17:38:54Z")

</div>

I’m using jQuery with Rails3.

I’d like to trigger some AJAX on the change of a SELECT element.

I’ve tried the old Prototype way: {:onchange  
=\>remote\_function(:update =\> “mytable”, :url =\> “/xxx”)}

I get a JS error: “Ajax is not defined”

I assume it’s trying to use Prototype and that’s where the error comes  
from.

I’d love to use Unobtrusive Javascript, but I don’t want to submit the  
WHOLE form. I just want the ONCHANGE of a SELECT element to trigger  
AJAX and update a specific piece of the page.

I can’t find any documentation anywhere that describes how to do this.

Anyone know any resources or know how to do this?

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 27, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/how-can-i-do-an-onchange-event-with-rails3-and-jquery/194001/2 "2010-08-27T17:41:50Z")

</div>

On Fri, Aug 27, 2010 at 10:28 AM, Andy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using jQuery with Rails3.
> 
> I’d like to trigger some AJAX on the change of a SELECT element.
> 
> Anyone know any resources or know how to do this?

function foo()  
{  
$.ajax(…);  
}

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [August 27, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/how-can-i-do-an-onchange-event-with-rails3-and-jquery/194001/3 "2010-08-27T18:06:02Z")

</div>

Andy wrote:

> I’m using jQuery with Rails3.
> 
> I’d like to trigger some AJAX on the change of a SELECT element.
> 
> I’ve tried the old Prototype way: {:onchange  
> =\>remote\_function(:update =\> “mytable”, :url =\> “/xxx”)}
> 
> I get a JS error: “Ajax is not defined”
> 
> I assume it’s trying to use Prototype and that’s where the error comes  
> from.
> 
> I’d love to use Unobtrusive Javascript, but I don’t want to submit the  
> WHOLE form.  
> […]

What’s that got to do with it? You can still do the onchange handler  
unobtrusively (and you should). Just use the DOM to find the element  
you need, and apply the handler.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
