# Dynamically updating a listbox from another listbox

**URL:** <https://www.ruby-forum.com/t/dynamically-updating-a-listbox-from-another-listbox/97451>\
**Category:** Rails\
**Created:** [July 7, 2006, 5:29am UTC](https://www.ruby-forum.com/t/dynamically-updating-a-listbox-from-another-listbox/97451 "2006-07-07T05:29:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@blair](https://www.ruby-forum.com/u/blair)\
**Post date:** [July 7, 2006, 5:29am UTC](https://www.ruby-forum.com/t/dynamically-updating-a-listbox-from-another-listbox/97451/1 "2006-07-07T05:29:04Z")

</div>

Hi,

I would like to be able to dynamically (without reloading the page)  
change the values from one listbox when a user select items from another  
listbox. I have a model ‘Sector’ which has\_many ‘levels’ (and of cours,  
‘Level’ beyong\_to ‘sector’). I want to show the user two listboxes with  
the sectors and levels. But when a user select a certain ‘sector’, the  
‘levels’ listbox should be populated dynamically with only the ones  
related to that sector. I’m using collection\_select for my listboxes.

Is it possible (with some AJAX maybe)?

Thanks

---

<div class="post-metadata">

**Author:** ![blair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@blair](https://www.ruby-forum.com/u/blair)\
**Post date:** [July 7, 2006, 5:35am UTC](https://www.ruby-forum.com/t/dynamically-updating-a-listbox-from-another-listbox/97451/2 "2006-07-07T05:35:15Z")

</div>

On 7 Jul 2006 03:27:47 -0000, Eric B. \<  
removed\_email\_address@domain.invalid\> wrote:

> Is it possible (with some AJAX maybe)?

Technoweenie has posted a tip for this on rails weenie

[http://rails.techno-weenie.net/tip/2006/6/30/prefill-a-select-box-from-an-ajax-request](http://rails.techno-weenie.net/tip/2006/6/30/prefill-a-select-box-from-an-ajax-request)

Thanks

---

<div class="post-metadata">

**Author:** ![blair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@blair](https://www.ruby-forum.com/u/blair)\
**Post date:** [July 7, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/dynamically-updating-a-listbox-from-another-listbox/97451/3 "2006-07-07T16:22:12Z")

</div>

On Friday, July 07, 2006, at 3:27 AM, Eric B. wrote:

> Is it possible (with some AJAX maybe)?  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> This is how I did it recently (using the KRJS plugin).

## View

…

 Container Type \<%= select\_tag 'container\_type\_id', options\_for\_select(['==Please Select=='] + ContainerType.to\_dropdown(:conditions=\>'can\_hold\_samples = true', :order=\>:name)) %\>

 What Container? \<%= select 'reagent','container\_id', [] %\>
...
## Controller

def on\_container\_type\_id\_change  
render :update do |page|  
@containers =  
Container.find\_all\_by\_container\_type\_id(params[:dom\_value],  
:order=\>‘name’)  
page.replace\_html ‘container-select’, :inline=\>“What Container?\<%= select  
‘reagent’,‘container\_id’, @containers.map {|o| [o.title, o.id]} %\>”  
end  
end

## 

The KRJS plugin automagically creates an AJAX call for an ‘onChange’  
event on ‘container\_type\_id’, which then gets called in the controller

Probably the easiest implementation of this I’ve seen. Just remember to  
replace the entire select div.

\_Kevin

---

<div class="post-metadata">

**Author:** ![blair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@blair](https://www.ruby-forum.com/u/blair)\
**Post date:** [July 7, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/dynamically-updating-a-listbox-from-another-listbox/97451/4 "2006-07-07T18:33:34Z")

</div>

On Friday, July 07, 2006, at 2:19 PM, Kevin O. wrote:

> > related to that sector. I’m using collection\_select for my listboxes.  
> > Rails mailing list  
> > \<%= select\_tag ‘container\_type\_id’, options\_for\_select(['==Please
> 
> ## Controller
> 
> \_Kevin
> 
> –  
> Posted with [http://DevLists.com](http://DevLists.com). Sign up and save your mailbox.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

I also wrote up a quick blog about this since it seems to come up so  
often.

[http://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/](http://www.sciwerks.com/blog/2006/07/07/updating-select-controls-with-ajax/)

\_Kevin
