# Observe Field Problem

**URL:** <https://www.ruby-forum.com/t/observe-field-problem/160302>\
**Category:** Rails\
**Created:** [February 14, 2009, 12:39am UTC](https://www.ruby-forum.com/t/observe-field-problem/160302 "2009-02-14T00:39:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enzor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbe5ce/32.png) [@enzor](https://www.ruby-forum.com/u/enzor)\
**Post date:** [February 14, 2009, 12:39am UTC](https://www.ruby-forum.com/t/observe-field-problem/160302/1 "2009-02-14T00:39:21Z")

</div>

Hi there!

I need to use observe field to change dynamically one select field based  
on another select field:

here is the view:

\<%= observe\_field ‘class’, :url =\> { :action =\> ‘update\_list’ },  
:frequency =\> 0.25,  
:update =\> ‘nickname’,  
:with =\> ‘tipid’  
%\>

Class:\<select name=“\<%= “#{name}[][#{name}\_class]” %\>” id=“class”\>  
\<%= options\_from\_collection\_for\_select(Tiprod.find(:all),  
“id”,“name”, {:include\_blank =\> true}) %\>  
  
Name:\<select name=“\<%= “#{name}[][#{name}\_data]” %\>” id=“nickname”\>

here is the controller

def update\_list  
@tipid = params[:tipid]  
@products = Specprod.find(:all, :conditions =\> [‘tiprif LIKE ?’,  
“%{@tipid}%”])  
end

and here is the update\_list.rhtml

\<% for product in @products%\>

 \<%= product.name%\> \<% end %\>

inspired by:  
[http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-elements-in-rails-using-ajax/](http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-elements-in-rails-using-ajax/)

Now, when i try to fire this application, the script doesn’t work, and  
the JS console tell me “Element is null” .  
Why?Where i was wrong?

thanks in advance for the help

---

<div class="post-metadata">

**Author:** ![enzor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/bbe5ce/32.png) [@enzor](https://www.ruby-forum.com/u/enzor)\
**Post date:** [February 14, 2009, 11:32am UTC](https://www.ruby-forum.com/t/observe-field-problem/160302/2 "2009-02-14T11:32:52Z")

</div>

On Feb 13, 11:39 pm, Enzo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> :with => 'tipid'
> 
> ```
> 
> here is the controller
> 
> \<%= product.name%\> \<% end %\>
> 
> inspired by:[http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-](http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-)…

The observe\_field helper just produces some javascript that does stuff  
to the element with id class

You’ve got the call to observe\_field before your select element, ie  
it’s trying to observe something that doesn’t exist yet.

Fred
