# RJS in Internet Explorer to update a list box

**URL:** <https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097>\
**Category:** Rails\
**Created:** [August 10, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097 "2006-08-10T18:09:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [August 10, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097/1 "2006-08-10T18:09:27Z")

</div>

Hi,

I’m trying some RJS to update a series of list boxes in which the user  
selects a state, and the following list gets updated with a list of  
counties, and the same for the next list of areas.

My code works perfectly (albeit a bit slow) on Firefox, but on Internet  
Explorer it clears the list box (instead of filling it) and Netscape  
shows all the counties cramped together on one instead of  
multiple options.

This is the relevant code from the form:

Select a  
state  
Select a  
county  
\<%= observe\_field “county\_state\_id”, :url =\> {:action =\>  
‘update\_counties\_list’}, :with =\> ‘county\_state\_id’ %\>  
\<%= observe\_field “area\_county\_id”, :url =\> {:action =\>  
‘update\_areas\_list’}, :with =\> ‘area\_county\_id’ %\>

And on the controller:

def update\_counties\_list  
render :update do |page|  
if params[‘county\_state\_id’].blank?  
page.replace\_html ‘address\_area\_id’, ‘Select a  
county’  
page.replace\_html ‘colonia\_municipio\_id’, ‘Select a  
state’  
else  
@state = State.find params[‘county\_state\_id’], :include  
=\> ‘counties’  
page.replace\_html ‘area\_county\_id’, ‘’

- options\_from\_collection\_for\_select(@state.counties, ‘id’, ‘name’)  
end  
end  
end

Upon inspecting HTTP traffic it seems the JavaScript code is fine (it  
works on Firefox after all), and neither IE nor Netscape show any error  
whatsoever.

Weird thing is, on IE, if I open the RJS URL directly, sometimes it  
works (showing the JS code), sometimes it doesn’t, showing an error  
message saying it can’t download the url (the requested site is either  
unavailable or cannot be found).

I’m on edge #4716 and I’ve tested on IE 6 & 7 beta 3.

I hope someone can help me ☹

Kind regards,  
Ivan V.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097/2 "2006-08-15T14:19:00Z")

</div>

On 10 Aug 2006, at 18:09, Ivan V. wrote:

> Weird thing is, on IE, if I open the RJS URL directly, sometimes it  
> works (showing the JS code), sometimes it doesn’t, showing an error  
> message saying it can’t download the url (the requested site is either  
> unavailable or cannot be found).
> 
> I’m on edge #4716 and I’ve tested on IE 6 & 7 beta 3.
> 
> I hope someone can help me ☹

IE goes wonky when trying to replace in the inner elements of  
 tags. You’ll be better off replacing either the whole  
 using page.replace instead of page.replace\_html or just  
wrapping the select itself in a div or span element, so you can use  
that in your page.replace\_html. This last method works, I’m using it  
in one of my apps. It’s not really the ideal solution for semantic  
XHTML markup, but sometimes you need to bend the rules a bit if that  
makes it work on all browsers.

Also, instead of rendering your element in your controller, use a  
render :partial passing an object (so you can use the name of the  
partial as a variable, all selects can be rendered using the same  
partial), your code would be a lot more DRY.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097/3 "2006-08-15T14:20:40Z")

</div>

why not try using an onchange handler in your select list, rather than  
observe\_field:

```
  <select id="user_country_id" name="user[country_id]"

```

onchange="\<%= remote\_function( :update =\> ‘locality’, :url =\> {  
:action =\> :change\_country }, :with =\> “‘user\_country=’ +  
$F(‘user\_country\_id’)”) %\>"\>  
\<%= options\_from\_collection\_for\_select(@countries, ‘id’,  
‘country\_name’, @locality.id ) %\>

Mike

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [January 31, 2007, 11:02am UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097/4 "2007-01-31T11:02:14Z")

</div>

Hi mike,

could you please explain the flow in detail for the below, I am new to  
ruby on rails.

And also i explained my requirement. i have a table called “locations”  
which holds the fields location\_code and location\_name. And i displayed  
that location\_name in list box. Also i have another table called  
“userdetails” which has field called country\_code. I want to update the  
country\_code in “userdetails” table based on the selection of list box  
it fetches the location\_code.

> ```
> <select id="user_country_id" name="user[country_id]"
> 
> ```
> 
> onchange="\<%= remote\_function( :update =\> ‘locality’, :url =\> {  
> :action =\> :change\_country }, :with =\> “‘user\_country=’ +  
> $F(‘user\_country\_id’)”) %\>"\>  
> \<%= options\_from\_collection\_for\_select(@countries, ‘id’,  
> ‘country\_name’, @locality.id ) %\>

regards  
Senthil

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [January 31, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097/5 "2007-01-31T12:01:14Z")

</div>

Ivan V. wrote:

> Hi,
> 
> I’m trying some RJS to update a series of list boxes in which the user  
> selects a state, and the following list gets updated with a list of  
> counties, and the same for the next list of areas.  
> …

I’ve posted some days ago ([Ajax - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/95361#196262))  
a code that replaces, using RJS, select option values. See if it can be  
useful for you.

page[‘field\_id’].length=0  
@your\_object.each do |obj|  
page \<\< “$(‘field\_id’).options[$(‘field\_id’).length]=new  
Option(‘#{escape\_javascript(obj.to\_s)}’,#{obj.id})”  
end

Regards  
Massimo  
[http://www.addsw.it](http://www.addsw.it)

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [February 1, 2007, 6:39am UTC](https://www.ruby-forum.com/t/rjs-in-internet-explorer-to-update-a-list-box/102097/6 "2007-02-01T06:39:45Z")

</div>

Hi Massimo,

I am updating the value in some other way, which seems to be quite  
simple.

in controller:

@object=classname.find(:all).map {|l| [l.field\_name, l.field\_code]}

in view:

Country  
\<%= select(:object2, :field\_code, @object1) %\>

here object1 is a parent table and object2 is a child table. based the  
change of the value in object1 the object2 will get updated.

Now problem is different the i want display the value for the selection  
in seperate text field. ie., how to get the code for the selected name.

regards  
Senthil
