# Multiple (AJAX) Observers on the Same Field and MSIE

**URL:** <https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174>\
**Category:** Rails\
**Created:** [August 16, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174 "2006-08-16T17:20:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/1 "2006-08-16T17:20:06Z")

</div>

I have been using multiple observers, i.e., observe\_field(), on the same  
input field and relying on them to execute in the same order that they  
appear in the page. This has been working fine in FireFox, but it does  
not seem to work in MSIE; the requests come in and are processed in a  
different order. Now, I’ve always been a little hesitant about using  
this technique, but it always seemed to work. My question is, is there a  
better way to do this?

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/2 "2006-08-16T18:45:31Z")

</div>

Hi Jason,

Jason F. wrote:

> I have been using multiple observers, i.e., observe\_field(),  
> on the same input field and relying on them to execute in  
> the same order that they appear in the page.

You’ve got me curious… Why multiple observers on the same field? I  
assume you’re observing for the same event given your “relying on them  
to  
execute…” statement. If so, then the question would be “why not put  
all  
the code in one place in the order you need it to execute?”

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/3 "2006-08-16T19:10:47Z")

</div>

Bill W. wrote:

> You’ve got me curious… Why multiple observers on the same field? I  
> assume you’re observing for the same event given your “relying on them  
> to execute…” statement. If so, then the question would be “why not put  
> all the code in one place in the order you need it to execute?”

I am using the multiple observers so that I can update multiple sections  
of the page when the input field changes, e.g., user checks a checkbox  
and both DIV1 and DIV2 are updated, but the update logic for DIV2  
depends on changes performed in the action that handles updating DIV1.  
The multiple observer method was a bit messy but always seemed to work  
(except in MSIE). – However, I think I’ve figured out a better way to  
do it after doing some more digging.

There is an update\_element\_function Prototype helper that I can embed in  
the partial returned by code that updates DIV1, that, when reached, will  
execute a second AJAX call to update DIV2. I think this will reliably  
“chain” my updates together in a way that will work in all browsers.  
Thoughts?

Thanks for the response!

Regards,  
Jason

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/4 "2006-08-16T20:00:40Z")

</div>

Hi Jason,

Jason F. wrote:

> I am using the multiple observers so that I can  
> update multiple sections of the page when the  
> input field changes

> Thoughts?

I’m thinking you’re using the :update option on observe\_field. If  
that’s  
right, I’m thinking … Don’t. Use RJS (pick up Cody F.'s $10 PDF  
book(let) on O’Reilly). Then you’ll only need one observer and you can  
update the page to your heart’s content from the RJS template. Or maybe  
I’m  
missing something about what you’re trying to accomplish. In which  
case, I  
apologize for my density 😉

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/5 "2006-08-16T21:54:55Z")

</div>

I think this link will solve the problem.  
and it is nice coding style too ;).

[http://rails.techno-weenie.net/tip/2006/6/20/cascading\_select\_boxes\_with\_rjs\_even\_on\_ie](http://rails.techno-weenie.net/tip/2006/6/20/cascading_select_boxes_with_rjs_even_on_ie)

Yours,  
Retonator

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/6 "2006-08-16T21:00:45Z")

</div>

Hi

I have three drop downs (State, City, Locality) and would like to  
populate  
City based on State selection and Locality based on City selection.

I have observers on two fields (State, and City). The observer\_field on  
state  
works fine and it populates city based on state selection. But the  
observer  
on city doesn’t populate Locality. Now sure whats wrong with my code.

here is the code snippet

RHTML Code:

 \<% @states.each do |state| %\> \<%= state.state\_name %\> \<% end %\> 
 Select City 
 \<%= observe\_field("state[id]", :update =\> "city\_container", :url =\> { :action =\> :select\_city }, :with =\> "'id='+value", :on =\> "changed") %\> 
 Select Locality 
 \<%= observe\_field("city\_id", :update =\> "locality\_container", :url =\> { :action =\> :select\_locality } :with =\> "'id='+value", :on =\> "changed") %\>

Controller Code

def select\_city  
@cities = City.find\_all\_by\_state\_id(@params[“id”])  
@html = “”  
@html += “Select City”  
@cities.each do |@city|  
@html += “#{@city.city\_name}”  
end  
@html += “”  
render\_text @html  
end

def select\_locality  
@localities = Locality.find\_all\_by\_city\_id(@params[“id”])  
@html = “”  
@html += “Select Locality”  
@localities.each do |@locality|  
@html += “#  
{@locality.locality\_name}”  
end  
@html += “”

# render\_text “test”

```
   render_text @html

```

end

I tried with render\_text “test” just to make sure that i get the text  
“test”  
back on selecting City. But that also doesn’t work. Is there a work  
around?

Thx for your help,  
Ramanan

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 17, 2006, 1:31am UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/7 "2006-08-17T01:31:18Z")

</div>

Jason F. wrote:

> Anyways, thanks for the pointer! It really helped!

You’re welcome. And congrats !!!

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 17, 2006, 1:55am UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/8 "2006-08-17T01:55:53Z")

</div>

Gerben van de Wiel wrote:

> I think this link will solve the problem.  
> and it is nice coding style too ;).
> 
> [http://rails.techno-weenie.net/tip/2006/6/20/cascading\_select\_boxes\_with\_rjs\_even\_on\_ie](http://rails.techno-weenie.net/tip/2006/6/20/cascading_select_boxes_with_rjs_even_on_ie)
> 
> Yours,  
> Retonator

I could not make it to work. May be I am doing something wrong. The  
comments for application\_helper.rb says that “It assumes that the  
function update\_select\_options is already in the open document”. What  
does this mean?

Do I need to explicitly include “update\_select\_options” code in my  
document or is this code is generated by application\_helper.rb. Can  
somebody tell me how to include “update\_select\_options” in my document.

Thx.  
Ramanan

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [August 16, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/multiple-ajax-observers-on-the-same-field-and-msie/65174/9 "2006-08-16T23:09:07Z")

</div>

Bill W. wrote:

> I’m thinking you’re using the :update option on observe\_field. If  
> that’s right, I’m thinking … Don’t. Use RJS (pick up Cody F.'s $10 PDF  
> book(let) on O’Reilly). Then you’ll only need one observer and you can  
> update the page to your heart’s content from the RJS template. Or maybe  
> I’m missing something about what you’re trying to accomplish. In which  
> case, I apologize for my density 😉

Yes, I was using the :update option on observe\_field. – I had played  
around with RJS templates before but could never get them to work for  
some reason. I took your advice, though, and purchased the PDF from  
O’Reilly on RJS templates.

After a quick read-through I was able to consolidate my code and really  
clean things up nicely! Whatever problems I had in the past with RJS  
templates are now gone. I think it might have had something to do with  
me not knowing to run the rake rails:update script. RJS templates are a  
very nice addition to the Rails framework.

Anyways, thanks for the pointer! It really helped!

Regards,  
Jason
