# How to: update a select from another select

**URL:** <https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194>\
**Category:** Rails\
**Created:** [June 1, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194 "2006-06-01T12:15:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [June 1, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194/1 "2006-06-01T12:15:44Z")

</div>

Hi all,

I woke up at 4AM this morning to find a solution for this and it is  
still not working… grrr! lost sleep for nothing!

Anyways, I have 2 select list and I want the content of the second list  
to be updated according to what is selected in the first list.

I did this in the past by having my application generate all the  
possible content of the second list and make some Javascript to do the  
change. But this time, I cant since these list can have more than 100  
elements each. So I need the first select to call an action from my  
controller that would then return the string to be inserted in the  
select.

Anyone has any idea/exemple how I could to this?

Thanks!

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [June 1, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194/2 "2006-06-01T15:00:07Z")

</div>

I just made a quick and dirty example for you:

Controller:  
def select

end

def set\_value  
if @params[:value].to\_i == 1  
render\_text ’ choice 1-1 choice 1-2 ’  
else  
render\_text ’ choice 2-1 choice 2-2 ’  
end  
end

View:

\<%= javascript\_include\_tag "prototype" %\> \<%= javascript\_include\_tag "scriptaculous" %\>
Select here:

{:action =\> "set\_value"}, :with =\> "'value=' + escape(value)" ) %\>" \> val 1 val 2 

Result here

Hope this helps.

Regards,

Nicolas

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [June 2, 2006, 12:07am UTC](https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194/3 "2006-06-02T00:07:10Z")

</div>

Thanks Nicolas,

Reviewing your code and reading a bit more, I finaly managed to make it  
work by using an observer on the list. So when the value changes, the  
observer call the action and update the other object.

Your code allowed me to learn about the :width option! Never read about  
it before 😉

Thanks again

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [June 2, 2006, 11:47am UTC](https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194/4 "2006-06-02T11:47:30Z")

</div>

the :with =\> “‘value=’ + escape(value)” is precious! Never quite  
understood  
how it works so well, but it does a great job.

---

<div class="post-metadata">

**Author:** ![alain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a183cd/32.png) [@alain](https://www.ruby-forum.com/u/alain)\
**Post date:** [June 2, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/how-to-update-a-select-from-another-select/62194/5 "2006-06-02T14:07:09Z")

</div>

There are issues with doing this in IE due to the lack of support for  
the js  
innerHTML method.

Ezra has provided a solution but your mileage may vary.

[http://www.ruby-forum.com/topic/61899#62762](http://www.ruby-forum.com/topic/61899#62762)
