# Using onchange options in forms

**URL:** <https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104>\
**Category:** Rails\
**Created:** [March 29, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104 "2006-03-29T18:02:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafuzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rafuzo](https://www.ruby-forum.com/u/rafuzo)\
**Post date:** [March 29, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104/1 "2006-03-29T18:02:41Z")

</div>

Hello,

I’m trying to get a select field to update a view via the ‘onchange’  
option. I’ve seen examples of it here in the forum where people hardcode  
the forms and use javascript to trigger the change (and nearly all have  
complained that it’s a hack) and I’m wondering if anyone has a more  
elegant solution using the embedded ruby methods? I’ve tried adding the  
“onchange”=\>“some\_action” argument to my select() but either I’m not  
using it properly or it doesn’t work. Does anyone have a nice solution  
using the select() method? (Or at least a way I could check if the html  
option listed above is actually working?)

Thanks…  
Bry M.

---

<div class="post-metadata">

**Author:** ![rafuzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rafuzo](https://www.ruby-forum.com/u/rafuzo)\
**Post date:** [March 29, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104/2 "2006-03-29T18:23:09Z")

</div>

> -----Original Message-----  
> I’m wondering if anyone has a more elegant solution using the  
> embedded ruby methods? I’ve tried adding the  
> “onchange”=\>“some\_action” argument to my select() but either  
> I’m not using it properly or it doesn’t work.

\<%=select :item, :attribute, collection.collect {|key,value| [value,key]  
},  
{}, {:onChange =\> ‘javascript:updateView()’} %\>

Will result in:

[…]

The empty hash contains options specific to the function, such as  
:include\_blank

Was it what you were asking for?

---

<div class="post-metadata">

**Author:** ![rafuzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rafuzo](https://www.ruby-forum.com/u/rafuzo)\
**Post date:** [March 29, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104/3 "2006-03-29T19:29:47Z")

</div>

Nicolas Malbranche wrote:

> > -----Original Message-----  
> > I’m wondering if anyone has a more elegant solution using the  
> > embedded ruby methods? I’ve tried adding the  
> > “onchange”=\>“some\_action” argument to my select() but either  
> > I’m not using it properly or it doesn’t work.
> 
> \<%=select :item, :attribute, collection.collect {|key,value| [value,key]  
> },  
> {}, {:onChange =\> ‘javascript:updateView()’} %\>
> 
> Will result in:
> 
> […]
> 
> The empty hash contains options specific to the function, such as  
> :include\_blank
> 
> Was it what you were asking for?

Almost - instead of the “updateView()” javascript method I’m looking to  
call a controller action.

Here’s my select code - the onChange call doesn’t seem to work, so maybe  
you’ll see something I’m doing wrong.  
the view:

\<%= select :timespan, :span, [“1 week”, “1 month”, “3 months”, “6  
months”, “1 year”], {}, {:onChange=\>‘javascript:updateView()’} %\>

generates the following html:

1 week 1 month 3 months 6 months 1 year

Thanks for your help

---

<div class="post-metadata">

**Author:** ![rafuzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rafuzo](https://www.ruby-forum.com/u/rafuzo)\
**Post date:** [April 3, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104/4 "2006-04-03T23:06:49Z")

</div>

Nicolas Malbranche wrote:

> Bry M. wrote:
> 
> > Almost - instead of the “updateView()” javascript method I’m looking to  
> > call a controller action.
> 
> Sorry for the delay, was out of town. OK, so as I understand it, this is  
> what you need:
> 
> \<%= select :timespan, :span, [“1 week”, “1 month”, “3 months”, “6  
> months”, “1 year”], {}, {:onChange=\>remote\_function(:update =\> {  
> :success =\> “successBlock”, :failure =\> “errorBlock” }, :url =\> {  
> :controller =\> “MyController”, :action =\> “update\_view”)} %\>
> 
> This is for the code on the client side. Your page need to contain two  
> containers (i.e. DIVs) named successBlock and errorsBlock. On the server  
> side, your controller needs to have a function called update\_view.

ok - I’m a little unclear on the mechanics here, though. Can you explain  
how the :update args interact with the url? I understand what’s going on  
in :url but I’m confused with how it works with :update. Where can I  
find ‘remote\_function’ defined in the source, also?

> In your current code, I assume nothing happens because you don’t have a  
> javascript function called updateView()?

That’s correct, mainly because I’m having another problem calling  
javascript inline in my view. I don’t plan on implementing things this  
way if I can do it in ruby, but I’m just trying to do a little “proof of  
concept” with js and not having much luck…

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![rafuzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rafuzo](https://www.ruby-forum.com/u/rafuzo)\
**Post date:** [March 30, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/using-onchange-options-in-forms/90104/5 "2006-03-30T21:41:04Z")

</div>

Bry M. wrote:

> Almost - instead of the “updateView()” javascript method I’m looking to  
> call a controller action.

Sorry for the delay, was out of town. OK, so as I understand it, this is  
what you need:

\<%= select :timespan, :span, [“1 week”, “1 month”, “3 months”, “6  
months”, “1 year”], {}, {:onChange=\>remote\_function(:update =\> {  
:success =\> “successBlock”, :failure =\> “errorBlock” }, :url =\> {  
:controller =\> “MyController”, :action =\> “update\_view”)} %\>

This is for the code on the client side. Your page need to contain two  
containers (i.e. DIVs) named successBlock and errorsBlock. On the server  
side, your controller needs to have a function called update\_view.

In your current code, I assume nothing happens because you don’t have a  
javascript function called updateView()?

Hope that helps.
