# Can RoR's AJAX helpers do this?

**URL:** <https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473>\
**Category:** Rails\
**Created:** [March 21, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473 "2006-03-21T15:24:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 21, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473/1 "2006-03-21T15:24:47Z")

</div>

Hi,

I want to type text into an input field then on enter, use AJAX to  
update the value of an input field elsewhere on the screen.

‘observe\_field’ allows me to watch a form field and dynamically update a  
DIV. Is there a variation or other method to allow me to update a field  
instead?

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 21, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473/2 "2006-03-21T15:53:51Z")

</div>

Lindsay B. wrote:

> Is there a variation or other method to allow me to update a field  
> instead?

I have raw AJAX code that will do this. If there isn’t a RoR helper to  
provide this functionality, is there a recommended RoR approach to  
embedding Javascript in an app?

thanks  
Lindsay

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 21, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473/3 "2006-03-21T16:01:12Z")

</div>

On Mar 21, 2006, at 15:24, Lindsay B. wrote:

> I want to type text into an input field then on enter, use AJAX to  
> update the value of an input field elsewhere on the screen.
> 
> ‘observe\_field’ allows me to watch a form field and dynamically  
> update a  
> DIV. Is there a variation or other method to allow me to update a  
> field  
> instead?

Just for the record, note that it does not need to be a div, it  
updates the content of any element with that id in that page. For  
instance, to update the contents of some paragraph just assign an id  
to it:

```
 <%= observe_field('foo', :url => { :action =>

```

‘my\_ajax\_updater’ }) %\>

…

Inline elements can be changed directly or wrapped in ad-hoc spans as  
well.

Is observe\_field then suitable for your needs?

– fxn

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 21, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473/4 "2006-03-21T17:07:14Z")

</div>

Xavier N. wrote:

> Is observe\_field then suitable for your needs?

Yes, it is. Thanks Xavier!

---

<div class="post-metadata">

**Author:** ![Lindsay\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ecae2f/32.png) [@Lindsay\_B](https://www.ruby-forum.com/u/Lindsay_B)\
**Post date:** [March 21, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/can-rors-ajax-helpers-do-this/55473/5 "2006-03-21T16:09:51Z")

</div>

On Mar 21, 2006, at 16:00, Xavier N. wrote:

> Just for the record, note that it does not need to be a div, it  
> updates the content of any element with that id in that page. For  
> instance, to update the contents of some paragraph just assign an  
> id to it:
> 
> ```
> <%= observe_field('foo', :url => { :action =>  
> 
> ```
> 
> ‘my\_ajax\_updater’ }) %\>
> 
> …

Oh sorry, that would be

```
 <%=
   observe_field(
     'my_field',
     :url => { :action => 'my_ajax_updater' },
     :update => 'foo'
   )
 %>
 <!-- ... and 100 lines below ... -->
 <p id="foo">...</p>

```

– fxn
