# Creating AJAX on the fly

**URL:** <https://www.ruby-forum.com/t/creating-ajax-on-the-fly/122532>\
**Category:** Rails\
**Created:** [December 8, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/creating-ajax-on-the-fly/122532 "2007-12-08T22:58:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 8, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/creating-ajax-on-the-fly/122532/1 "2007-12-08T22:58:56Z")

</div>

I have two drop-down menus in a page.

the first has only two values, and when one of those two values is  
selected, an AJAX call is made to RoR.

there, I fetch some rows from the database, and fill the second  
drop-down menu.

but, from this second drop-down menu, I also want to make another AJAX  
call and fill some data in a Div.

The second drop-down menu fills correctly, but never fires the AJAX  
call.

First I thought it was because the second drop-down menu didn’t exist in  
the page, and the second observer couldn’t find in when the page was  
loaded.

For testing purposes, I created also the second pop-up with some  
ficticious data, and the AJAX call worked, so I suppose the second  
observer is working as expected.

Also I updated this false second drop-down menu with the new one, coming  
from the first AJAX call, but the same as before, this drop-down menu  
never fires the AJAX call.

I’m sure the id of the select is good.

What I’m thinking, is that an observer that has to observ a not yet  
created object, maybe can’t work, and no matter if the object existed,  
because when the first AJAX call replaces the other, even with the same  
id, it also dosn’t.

So, my workaround is creating the second drop-down menu with the option  
OnChange=:someJavascriptMethod();

This method works, when a new item is selected from the second menu, the  
javascript method fires, but I don’t know how to make a call using AJAX,  
I’m using this but I don’t know how to update only a

‘function year\_function()  
{  
var popup = document.getElementById(“user\_year”);  
location.href = ("/user/change\_year?year="+popup.value) ;  
}’

=\> any clue on how to send the response to an object, like a Div, …?

I know this question is more for an AJAX forum, but as I’m creating with  
the RoR helpers …

🙂

thanks,

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 9, 2007, 10:59am UTC](https://www.ruby-forum.com/t/creating-ajax-on-the-fly/122532/2 "2007-12-09T10:59:02Z")

</div>

hello again,

finally I found link\_to\_remote() wich solves the problem a little.

instead of crating a new drop-down menu with the customers found, I  
created a

 and link\_to\_remote each row. as the rows can be large,  
I’d prefer a drop-down menu …

If anyone knows how to generate by code an instance of a drop-down menu  
with an observer …

🙂

thanks,

raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 9, 2007, 11:18am UTC](https://www.ruby-forum.com/t/creating-ajax-on-the-fly/122532/3 "2007-12-09T11:18:37Z")

</div>

hello again …

well, finally works !!!

the observer must be in the same

 as the drop-down menu, so when  
RoR loads the new drop-down menu, at the end I create the observer, in a  
\_view.rhtml page …

it works !!

🙂

rai
