# Autocomplete for the chosen element from autocomplete

**URL:** <https://www.ruby-forum.com/t/autocomplete-for-the-chosen-element-from-autocomplete/88856>\
**Category:** Rails\
**Created:** [March 15, 2007, 1:07am UTC](https://www.ruby-forum.com/t/autocomplete-for-the-chosen-element-from-autocomplete/88856 "2007-03-15T01:07:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 15, 2007, 1:07am UTC](https://www.ruby-forum.com/t/autocomplete-for-the-chosen-element-from-autocomplete/88856/1 "2007-03-15T01:07:24Z")

</div>

Hi,

How could I achieve the following - I have autocomplete which is working  
pretty well, but after select any value from the list, I would like to  
get in the search box another list of values associated with already  
chosen value from the first list and to have autocomplete for the new  
list of values? Actually, something like Google suggest functionality  
(no, I am not going to launch Google suggest competition smile ), but I  
would like to learn and eventually to implement something similar  
locally for my web site.  
For instance, if I have autocomplete list with dog races and choose  
‘terrier’ from the list, I would like to get a new list with - let say  
people who have terriers or something like this.

Are there any articles/howto’s which could give the right  
comprehending/directionÃŸ

Thanks

---

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 16, 2007, 12:58am UTC](https://www.ruby-forum.com/t/autocomplete-for-the-chosen-element-from-autocomplete/88856/2 "2007-03-16T00:58:02Z")

</div>

On Mar 15, 2007, at 1:07 AM, Any Dot wrote:

> For instance, if I have autocomplete list with dog races and choose  
> ‘terrier’ from the list, I would like to get a new list with - let say  
> people who have terriers or something like this.

There is a completion option called “:after\_update\_element”. You can  
assign there a callback

function (e, v) { … }

and generate a second widget based on the value in its body.

– fxn

---

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 24, 2007, 9:34pm UTC](https://www.ruby-forum.com/t/autocomplete-for-the-chosen-element-from-autocomplete/88856/3 "2007-03-24T21:34:51Z")

</div>

> There is a completion option called “:after\_update\_element”. You can  
> assign there a callback
> 
> function (e, v) { … }
> 
> and generate a second widget based on the value in its body.
> 
> – fxn

Can anyone offer some sample code on how to do this. I am a perl guru  
but when it comes to javascript I’m a weenie.

---

<div class="post-metadata">

**Author:** ![anydot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@anydot](https://www.ruby-forum.com/u/anydot)\
**Post date:** [March 25, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/autocomplete-for-the-chosen-element-from-autocomplete/88856/4 "2007-03-25T12:29:57Z")

</div>

On Mar 24, 2007, at 9:34 PM, psyber wrote:

> Can anyone offer some sample code on how to do this. I am a perl guru  
> but when it comes to javascript I’m a weenie.

Sure.

That parameter is a JavaScript function that receives two arguments.  
You can just build an ad-hoc anonymous subroutine for it, the  
following construction is the JavaScript analogous of sub { … } in  
Perl:

# this is a Ruby string that contains JavaScript to be passed

# to the helper, off the top of my head, untested

after\_update\_element\_js = \<\<-JS.gsub(/\s+/, ’ ‘)  
function(e, v) {  
new Ajax.Request(’/foo/bar’, {  
parameters: {value: e.value}, /\* e.value is the completion \*/  
asynchronous: true,  
evalScripts: true  
});  
}  
JS

And then:

\<%= some\_auto\_completion\_helper …, :after\_update\_element =\>  
after\_update\_element\_js %\>

Behind /foo/bar there’s a RJS that updates the second widget from  
params[:value].

– fxn
