# Observe\_field and select\_tag

**URL:** <https://www.ruby-forum.com/t/observe-field-and-select-tag/142981>\
**Category:** Rails\
**Created:** [July 14, 2008, 6:45am UTC](https://www.ruby-forum.com/t/observe-field-and-select-tag/142981 "2008-07-14T06:45:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 14, 2008, 6:45am UTC](https://www.ruby-forum.com/t/observe-field-and-select-tag/142981/1 "2008-07-14T06:45:31Z")

</div>

I have this select field:

Access  
\<%= f.select :access, [‘Global’, ‘Carrier’, ‘Winery’], :prompt =\>  
“Choose One” %\>

being observed by:

\<%= observe\_field :user\_access, :on =\> ‘blur’,  
:url =\> {:action  
=\> :find\_access}, :frequency =\> 0.25,  
:with =\> ‘value’ %\>

When the user selects an option like ‘Carrier’ I’d like an empty div  
to be replaced with a partial of carrier names… I’m doing this with:

page.replace\_html :user\_options, :partial =\>  
‘shipper\_options’, :object =\> @shippers

in the find\_access.rjs file. My problem is that if I have the select  
list open and roll over ‘Carrier’ it does the replace\_html on the  
div… is there a way to make it wait until the user actually selects  
the value ‘Carrier’?

Thanks for any help… this is my first foray into AJAX and I know  
this is probably a stupidly simple issue.

SH

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 14, 2008, 7:07am UTC](https://www.ruby-forum.com/t/observe-field-and-select-tag/142981/2 "2008-07-14T07:07:26Z")

</div>

shenry wrote:

> I have this select field:
> 
> Access  
> \<%= f.select :access, [‘Global’, ‘Carrier’, ‘Winery’], :prompt =\>  
> “Choose One” %\>
> 
> being observed by:
> 
> \<%= observe\_field :user\_access, :on =\> ‘blur’,  
> :url =\> {:action  
> =\> :find\_access}, :frequency =\> 0.25,  
> :with =\> ‘value’ %\>
> 
> When the user selects an option like ‘Carrier’ I’d like an empty div  
> to be replaced with a partial of carrier names… I’m doing this with:
> 
> page.replace\_html :user\_options, :partial =\>  
> ‘shipper\_options’, :object =\> @shippers
> 
> in the find\_access.rjs file. My problem is that if I have the select  
> list open and roll over ‘Carrier’ it does the replace\_html on the  
> div… is there a way to make it wait until the user actually selects  
> the value ‘Carrier’?
> 
> Thanks for any help… this is my first foray into AJAX and I know  
> this is probably a stupidly simple issue.
> 
> SH

The seleced value transmitted to the server is the current value of your  
selectbox. Untill an option is not clicked it is not selected.

I think a better event would be onchange instead of on blur, to trigger  
your ajax query. Like that no need to observe the field all the time, so  
i’ts better for the app.

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 14, 2008, 7:55am UTC](https://www.ruby-forum.com/t/observe-field-and-select-tag/142981/3 "2008-07-14T07:55:05Z")

</div>

I’ve changed the select field to be:

\<%= f.select :access, [‘Global’, ‘Carrier’, ‘Winery’], :prompt =\>  
“Choose One”,  
:onchange =\> remote\_function(:url =\> {:action  
=\> :find\_access}) %\>

nothing happens when changing the selection (I don’t see the onchange  
in the html source either…)

Should I be coding the javascript manually, rather than using helpers?

thx

SH

On Jul 13, 10:07 pm, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [July 14, 2008, 8:45am UTC](https://www.ruby-forum.com/t/observe-field-and-select-tag/142981/4 "2008-07-14T08:45:24Z")

</div>

D’oh… here is what works =\>

\<%= f.select :access, [‘Global’, ‘Carrier’, ‘Winery’], {:prompt =\>  
“Choose One”},  
:onchange =\> remote\_function(:url =\> {:action =\> :find\_access},  
:with =\> “‘value=’ + value”) %\>

wasn’t paying attention to the curly braces.

thanks nico,

SH
