Re: [Rails] How can I do an onChange event with Rails3 and jQuery?

94 views
Skip to first unread message

Greg Donald

unread,
Aug 27, 2010, 11:40:09 AM8/27/10
to rubyonra...@googlegroups.com
On Fri, Aug 27, 2010 at 10:28 AM, Andy <andy...@gmail.com> 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?

<select onchange="javascript:foo()">

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


--
Greg Donald
destiney.com | gregdonald.com

Andy

unread,
Aug 27, 2010, 11:28:25 AM8/27/10
to Ruby on Rails: Talk
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

Marnen Laibow-Koser

unread,
Aug 27, 2010, 12:06:02 PM8/27/10
to rubyonra...@googlegroups.com
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
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

Reply all
Reply to author
Forward
0 new messages