# Piggybacking an action on an autocompleted field

**URL:** <https://www.ruby-forum.com/t/piggybacking-an-action-on-an-autocompleted-field/56632>\
**Category:** Rails\
**Created:** [April 1, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/piggybacking-an-action-on-an-autocompleted-field/56632 "2006-04-01T23:51:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [April 1, 2006, 11:51pm UTC](https://www.ruby-forum.com/t/piggybacking-an-action-on-an-autocompleted-field/56632/1 "2006-04-01T23:51:43Z")

</div>

Hi everybody,

I’d like to add a custom action to be triggered after/when an  
autocompleted field has been/is beeing updated by the autocompletion  
process.  
I tried the obvious - a field observer -, but it doesn’t work (see code  
below). I know I could write the autocompletion code myself, but I  
wonder if there is a simpler/more elegant way?

Any idea?  
TIA

Alain

The observer solution (that doesn’t work):

```
<%= text_field_with_auto_complete :city, :zip %>
<%= observe_field("city_zip",
   :frequency => 0,
   :url => { :action => :get_names_for_zip }, <<<---- IS NEVER 

```

CALLED  
:with =\> “‘zip=’+value”) %\>

---

<div class="post-metadata">

**Author:** ![aravet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@aravet](https://www.ruby-forum.com/u/aravet)\
**Post date:** [April 2, 2006, 12:22pm UTC](https://www.ruby-forum.com/t/piggybacking-an-action-on-an-autocompleted-field/56632/2 "2006-04-02T12:22:52Z")

</div>

There’s already a tiny patch - 3691 - in the waiting list that solves  
that very problem, and lets you write  
\<%= text\_field\_with\_auto\_complete :city, :zip ,{},  
:after\_update\_element=\> “alert(‘after\_update\_element…’)”  
%\>

Alain

```
http://dev.rubyonrails.org/ticket/3691
[PATCH] Add frequency and afterupdateelement option to

```

text\_field\_with\_auto\_complete

It justs adds 2 lines in the _auto\_complete\_field_ method  
(java\_script\_macros\_helper.rb)  
js\_options[:frequency] = “#{options[:frequency]}” if  
options[:frequency]  
js\_options[:afterUpdateElement] = “function(element,value) {  
return #{options[:after\_update\_element]}(element,value); }” if  
options[:after\_update\_element]
