# How update a select onchange?

**URL:** <https://www.ruby-forum.com/t/how-update-a-select-onchange/76545>\
**Category:** Rails\
**Created:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/how-update-a-select-onchange/76545 "2006-11-20T14:45:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [November 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/how-update-a-select-onchange/76545/1 "2006-11-20T14:45:58Z")

</div>

Hey all,  
I have a select(“post”, “person\_id”, Person.find\_all.collect {|p|  
[p.name, p.id] }) and I would like to submit the value when it is  
selected.  
I know I have to do something like this: onchange="\<%=  
remote\_function(:update =\> “options”,  
:url =\> { :action =\> :update\_options })  
but how can I pass it the value of the selected option?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/how-update-a-select-onchange/76545/2 "2006-11-20T14:46:00Z")

</div>

remote\_function(:update =\> “options”,  
:url =\> { :action =\> :update\_options },  
:with =\> “‘post%5Bperson\_id%5D=’+$F(‘post\_person\_id’)”)

HTH,  
Pratik

On 11/19/06, Patrick A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanx in advance
> 
> Pat
> 
> >

–  
rm -rf / 2\>/dev/null - [http://null.in](http://null.in)

Dont judge those who try and fail, judge those who fail to try…

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [November 21, 2006, 12:20am UTC](https://www.ruby-forum.com/t/how-update-a-select-onchange/76545/3 "2006-11-21T00:20:48Z")

</div>

On 11/20/06, Pratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> remote\_function(:update =\> “options”,  
> :url =\> { :action =\> :update\_options },  
> :with =\> “‘post%5Bperson\_id%5D=’+$F(‘post\_person\_id’)”)

Hey Pratik, thanx for your anwser,  
basically, this is what I have:

No\<option value=“1”

> YesNo\<option value=“3”  
> Maybe

the poblem is that the updated value of confvalue is  
‘this[this.selectedIndex].value’ instead of the selected value.  
I also tried something like that:

:with=\>config[confvalue]‘=\>’+this[this.selectedIndex].value’

but in that case nothing get updated at all.  
any idea what’s wrong?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [November 21, 2006, 2:37am UTC](https://www.ruby-forum.com/t/how-update-a-select-onchange/76545/4 "2006-11-21T02:37:06Z")

</div>

On 11/20/06, Patrick A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ‘this[this.selectedIndex].value’ instead of the selected value.  
> Pat
> 
> >

Pat,

You can also use observe\_field().

\<%= select :post, :person\_id, Person.find(:all).collect {|p| [ p.name,  
p.id ] } %\>  
\<%= observe\_field :post\_person\_id, :url =\> { … }, :with =\>  
‘post\_person\_id’ %\>

Hope this helps,

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)  
[http://trackplace.com](http://trackplace.com)

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [November 26, 2006, 5:12am UTC](https://www.ruby-forum.com/t/how-update-a-select-onchange/76545/5 "2006-11-26T05:12:13Z")

</div>

Patrick,

Here is a select that will be submitted via AJAX request when changed:

\<%= select( :vehicle, :body\_type,  
@body\_types.collect {|bt| [bt, bt]},  
{},  
:onchange =\> remote\_function( :with =\>  
“‘vehicle[body\_type]=’+escape(value)”,  
:loading =\>  
“Element.show(‘loading-indicator4’)”,  
:complete =\>  
evaluate\_remote\_response,  
:url =\> { :action =\>  
:vehicle\_body\_type\_select } ) ) %\>

This will run the method ‘vehicle\_body\_type\_select’ in the current  
controller, and when that action is executed the currently selected  
value will be in ‘params[:vehicle][:body\_type]’ from the :with  
parameter. The method passes back Javascript via RJS statements which  
are evaluated by the :complete item.

David
