# Observe\_field with dynamic forms

**URL:** <https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416>\
**Category:** Rails\
**Created:** [December 31, 2005, 1:53am UTC](https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416 "2005-12-31T01:53:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [December 31, 2005, 1:53am UTC](https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416/1 "2005-12-31T01:53:56Z")

</div>

I have a form where I dynamically add form elements based on user input.  
An example is that when a user selects a “server” from a select box I  
add (via AJAX calls back to server) a “type” select box pre-populated  
with valid choices for that server. I do this by using observe\_field on  
the “server” field and update a div on the page with the new “type”  
select box. So far so good. The form submits with all the proper  
values.

Now I want to watch the newly added “type” field with observe\_field but  
it doesn’t work. I never see the callback function specified in  
observe\_field get called. It seems like since the element didn’t exist  
when the page initially loaded the observe\_field can’t watch the  
element. I also tried updating/adding the observe\_field code when I  
create the new “type” select box but that also doesn’t work.

I can whip up an example if ppl need to see it in code but it is rather  
difficult to extract it cleanly from my current code.

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [December 31, 2005, 2:53am UTC](https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416/2 "2005-12-31T02:53:12Z")

</div>

Jason W. wrote:

> Now I want to watch the newly added “type” field with observe\_field but  
> it doesn’t work. I never see the callback function specified in  
> observe\_field get called. It seems like since the element didn’t exist  
> when the page initially loaded the observe\_field can’t watch the  
> element. I also tried updating/adding the observe\_field code when I  
> create the new “type” select box but that also doesn’t work.

Yes, I don’t think observe\_field will work if the element does not  
exist at the time the observe\_field script section is executed.

Your second method of sending the observe field script section together  
with (after!) the select should work. Use the Mozilla/Firefox source  
browser (Ctrl-U) and DOM inspectors (Ctrl-Shift-I) to make sure what’s  
in the document is going to do what you think.

> I can whip up an example if ppl need to see it in code but it is rather  
> difficult to extract it cleanly from my current code.

Yes, you’ll have to post the code for both the initial and AJAX views  
if you still can’t get it to work.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [December 31, 2005, 3:23am UTC](https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416/3 "2005-12-31T03:23:25Z")

</div>

You could try using hidden fields for every possible observable field  
in the div (or whatever) that gets replaced via AJAX. Say you have  
three possible HTML pages, where:

- No server is selected
- Server 1 is selected
- Server 2 is selected

Then for “no server selected”, you would put in all the observable  
fields for server 1 and server 2 as blank hidden fields. If server 1  
is selected, then all the observable fields for server 2 are blank  
hidden fields in the returned HTML, and vice-versa for server 2.

It’s sloppy, and I’m not sure it’ll work (it’ll certainly give you  
headaches with your observable fields for server 2 when you are move  
from that to server 1) but it’s certainly worth a shot.

Dave

On 12/31/05, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> exist at the time the observe\_field script section is executed.  
> Yes, you’ll have to post the code for both the initial and AJAX views

–  
Site: [http://antidis.com/](http://antidis.com/)

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [December 31, 2005, 8:32pm UTC](https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416/4 "2005-12-31T20:32:18Z")

</div>

Mark Reginald J. wrote:

> Your second method of sending the observe field script section together  
> with (after!) the select should work. Use the Mozilla/Firefox source  
> browser (Ctrl-U) and DOM inspectors (Ctrl-Shift-I) to make sure what’s  
> in the document is going to do what you think.

But unfortunately the second method doesn’t seem to work.

Mozilla/Firefox view source doesn’t seem to show me the dynamically  
updated content. It just shows the initial view of the page. ☹ This  
hampers my debugging quite a bit and I end up running ethereal to see  
the code that ajax calls add in.

I’ll write up a sample and post it later today.

Thanks  
Jason

---

<div class="post-metadata">

**Author:** ![jasonwold](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e36b37/32.png) [@jasonwold](https://www.ruby-forum.com/u/jasonwold)\
**Post date:** [January 1, 2006, 12:03am UTC](https://www.ruby-forum.com/t/observe-field-with-dynamic-forms/51416/5 "2006-01-01T00:03:20Z")

</div>

You can see the dynamically updated DOM tree with the DOM inspector in  
the Web D. Toolbar for Firefox. That should help you.

Dave

On 12/31/05, Jason W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Site: [http://antidis.com/](http://antidis.com/)
