# f.select onchange determine other fields in view

**URL:** <https://discuss.rubyonrails.org/t/f-select-onchange-determine-other-fields-in-view/45475>\
**Category:** rubyonrails-talk\
**Created:** [August 25, 2009, 7:48pm UTC](https://discuss.rubyonrails.org/t/f-select-onchange-determine-other-fields-in-view/45475 "2009-08-25T19:48:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 25, 2009, 7:48pm UTC](https://discuss.rubyonrails.org/t/f-select-onchange-determine-other-fields-in-view/45475/1 "2009-08-25T19:48:36Z")

</div>

I am trying to create a view/form that has a dropdown

\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :type\_id%\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.select :type\_id,Type.all.collect{|c| [c.description,c.id]}, :onchange=\>"HERE I NEED TO DO SOMETHING WHAT?" %\> &nbsp;&nbsp;&nbsp;&nbsp;\<!-- on change or on select show drop down for value otherwise regular value --\> &nbsp;&nbsp;\</p\>

Now when they change and the id is now 5 HERE I NEED TO CHECK THE VALUE SELECTED BY THE FIRST SELECT 1. How do I access the id of the first select and what was selected?

I want to create: \<p\> &nbsp;&nbsp;&nbsp;\<%= f.label :value %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.select :value,Test.get\_tests.all.collect{|c| [c.description,c.test\_id]} %\> &nbsp;&nbsp;\</p\>

If its not 5 I want \<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :value %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :value %\>

Please if you can tell me how to access the first select- do I set an id? and what I do onchange since its in the same form a few lines down I want to have a different field.

Thank you

---

<div class="post-metadata">

**Author:** ![Bill\_Walton1](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bill\_Walton1](https://discuss.rubyonrails.org/u/Bill_Walton1)\
**Post date:** [August 26, 2009, 1:18am UTC](https://discuss.rubyonrails.org/t/f-select-onchange-determine-other-fields-in-view/45475/2 "2009-08-26T01:18:06Z")

</div>

Hi Tz Le,

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 26, 2009, 5:55pm UTC](https://discuss.rubyonrails.org/t/f-select-onchange-determine-other-fields-in-view/45475/3 "2009-08-26T17:55:28Z")

</div>

Tz Le wrote:

> bill walton wrote:
> 
> > Hi Tz Le,
> > 
> > > Now when they change and the id is now 5 If its not 5 I want \<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :value %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :value %\>
> > > 
> > > Please if you can tell me how to access the first select- do I set an id? and what I do onchange since its in the same form a few lines down I want to have a different field.
> > 
> > You need to put an observe\_field on the first field that makes an ajax call to render a partial containing the second field with the appropriate choices based on the first selection. Try a google search on 'rails observe\_field select'.
> > 
> > HTH,
> > 
> > Bill
> 
> Ok.Thank you, I will search for this.

I read about the observe field select. Two things: Every post I see determines a second select based on the first select. What I am trying to do is based on the first select either display a second select or display a text field(this will depend on the first select value)

I tried to use the observe field and I am not sure of two things: the action=\> 'funtion' Do I need to create in the controller any function and if yes what? basically if there is a change in the select, I want to check the value if it equals 5, display a second select, if not display text field.

Also what does the parameter :with do? The :update=\> "myfield name" this field should be the second select name and then how to I display the text field instead?

---

<div class="post-metadata">

**Author:** ![JL\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@JL\_Smith](https://discuss.rubyonrails.org/u/JL_Smith)\
**Post date:** [August 26, 2009, 7:40pm UTC](https://discuss.rubyonrails.org/t/f-select-onchange-determine-other-fields-in-view/45475/4 "2009-08-26T19:40:36Z")

</div>

Is this what you're trying to do?

> **[\#88 Dynamic Select Menus - RailsCasts](http://railscasts.com/episodes/88-dynamic-select-menus)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
