# Can you use observe\_field to watch a select list and then update the contents of a text field?

**URL:** <https://www.ruby-forum.com/t/can-you-use-observe-field-to-watch-a-select-list-and-then-update-the-contents-of-a-text-field/147705>\
**Category:** Rails\
**Created:** [September 4, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/can-you-use-observe-field-to-watch-a-select-list-and-then-update-the-contents-of-a-text-field/147705 "2008-09-04T13:37:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [September 4, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/can-you-use-observe-field-to-watch-a-select-list-and-then-update-the-contents-of-a-text-field/147705/1 "2008-09-04T13:37:02Z")

</div>

Before I waste more time trying to figure that out, I was wondering if  
it’s even possible? The Rails Way says that observe\_field by default  
will trigger changes in text fields and text areas, and on clicks for  
radio buttons and check boxes. So perhaps I’m using the wrong tool  
here? Thanks.

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [September 4, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/can-you-use-observe-field-to-watch-a-select-list-and-then-update-the-contents-of-a-text-field/147705/2 "2008-09-04T13:49:57Z")

</div>

On 4 Sep 2008, at 13:36, Sharon M. wrote:

> Before I waste more time trying to figure that out, I was wondering if  
> it’s even possible? The Rails Way says that observe\_field by default  
> will trigger changes in text fields and text areas, and on clicks for  
> radio buttons and check boxes. So perhaps I’m using the wrong tool  
> here? Thanks.

should do, but i’d just use an onChange handler.

Fred

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [September 7, 2008, 6:38am UTC](https://www.ruby-forum.com/t/can-you-use-observe-field-to-watch-a-select-list-and-then-update-the-contents-of-a-text-field/147705/3 "2008-09-07T06:38:25Z")

</div>

So it’s easy enough to pop some :onchange JavaScript code inline for a  
collection\_select drop-down list. That code adds value of what’s  
selected to a couple of other text fields:

Place\*: \<%=  
collection\_select(:event, :venue\_id, @venues\_all, :id, :name, {:prompt  
=\> " Please select a location"}, {:onchange =\> “var id = this.value;  
document.forms[0].elements[2].value=(id);document.forms[0].elements[3].value=document.forms[0].elements[5].value”})  
%\>

But I don’t want the venue value (id number) in the text fields; I  
want venue.phone and venue.url in those fields. After numerous hours  
the past few days, I still don’t understand how I can mix JavaScript  
and Ruby so I can access the venue.phone and venue.url values and have  
them update my form’s other text fields. Any advice appreciated.

On Sep 4, 7:49 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sharon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/67e7ee/32.png) [@sharon](https://www.ruby-forum.com/u/sharon)\
**Post date:** [September 7, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/can-you-use-observe-field-to-watch-a-select-list-and-then-update-the-contents-of-a-text-field/147705/4 "2008-09-07T12:14:48Z")

</div>

On Sep 7, 5:37 am, Sharon M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But I don’t want the venue value (id number) in the text fields; I  
> want venue.phone and venue.url in those fields. After numerous hours  
> the past few days, I still don’t understand how I can mix JavaScript  
> and Ruby so I can access the venue.phone and venue.url values and have  
> them update my form’s other text fields. Any advice appreciated.

One way would be to make an ajax call from the on change handler. The  
remote\_function helper is good for this. Something like  
remote\_function( :url =\> {…}, :with =\> “‘id=’+this.value”)

Should submit the selected id to the specified action. You can then  
you some rjs to update the appropriate form elements.

Fred
