# Text\_field\_with\_auto\_complete - Trigger on NonSelecting

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514>\
**Category:** Rails\
**Created:** [July 19, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514 "2008-07-19T21:46:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [July 19, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/1 "2008-07-19T21:46:03Z")

</div>

Hi,

I am using text\_field\_with\_auto\_complete on a field. I update a part of  
my page if user chooses any particular option listed by auto\_complete  
list. To do this I call and remote\_function with :after\_update\_element.

But I also wanna update a part of my page if user doesn’t select  
anything from the autocomplete list.

For EX: I want to Bring it to notice of the user that the “Name” that he  
has not entered is not in our database. The div like below should be  
higlighted

 Hospital entered was not found. Please enter the details here: 

So what I need to do is : When control moves out of the field on which  
auto\_complete is enacted, and when user doesn’t select any option listed  
by auto\_complete, I want to trigger a different action.

I tried to simulate this using text\_field\_with\_auto\_complete along with  
observe\_field :on =\> :change. It didn’t help, as onChange gets triggered  
on both cases: User selecting one option from the list and User entering  
a new Name.

How can I solve this? Please suggest.

Regards,  
SG

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [July 20, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/2 "2008-07-20T23:39:37Z")

</div>

On Sat, Jul 19, 2008 at 9:46 PM, Sandeep G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> observe\_field :on =\> :change. It didn’t help, as onChange gets triggered  
> on both cases: User selecting one option from the list and User entering  
> a new Name.

model\_auto\_completer  
([http://agilewebdevelopment.com/plugins/model\_auto\_completer](http://agilewebdevelopment.com/plugins/model_auto_completer)) needed  
something similar. In this case the feature is to rollback to the last  
valid completion if the user enters free text.

To accomplish this the textfield has a custom cache attribute. When  
the user selects some completion the cache is reset in  
:after\_update\_element. There’s an onblur handler that compares the  
cache to the current value, and rollbacks if they are different. In  
your case you’d show the warning.

That’s the basic idea, have a look at its implementation for further  
details.

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [July 21, 2008, 12:20am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/3 "2008-07-21T00:20:35Z")

</div>

On Sun, Jul 20, 2008 at 11:39 PM, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To accomplish this the textfield has a custom cache attribute. When  
> the user selects some completion the cache is reset in  
> :after\_update\_element. There’s an onblur handler

It is an onchange handler currently.

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [October 6, 2008, 9:06am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/4 "2008-10-06T09:06:13Z")

</div>

Hi Sandeep,  
Can you show me how to call a remote\_function  
with :after\_update\_element? I need almost the exact same  
functionality. Once a user chooses an option from the autocomplete  
list, i want to update a div with more information relating to the  
selected option. Also, did you find a solution for when the user  
doesnt select anything from the list?  
Also, how did you manage to parse the id of the selected option along  
with the form values? Mine is a belongs\_to association and so i am  
using model\_auto\_completer for now. I hope you can help me with this.  
Here is my post on this  
[http://groups.google.co.in/group/rubyonrails-talk/browse\_thread/thread/5a91ddc41c1c44d9](http://groups.google.co.in/group/rubyonrails-talk/browse_thread/thread/5a91ddc41c1c44d9)

On Jul 20, 12:46 am, Sandeep G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [October 6, 2008, 9:06am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/5 "2008-10-06T09:06:48Z")

</div>

Hi Sandeep,  
Can you show me how to call a remote\_function  
with :after\_update\_element? I need almost the exact same  
functionality. Once a user chooses an option from the autocomplete  
list, i want to update a div with more information relating to the  
selected option. Also, did you find a solution for when the user  
doesnt select anything from the list?  
Also, how did you manage to parse the id of the selected option along  
with the form values? Mine is a belongs\_to association and so i am  
using model\_auto\_completer for now. I hope you can help me with this.  
Here is my post on this  
[http://groups.google.co.in/group/rubyonrails-talk/browse\_thread/thread/5a91ddc41c1c44d9](http://groups.google.co.in/group/rubyonrails-talk/browse_thread/thread/5a91ddc41c1c44d9)

On Jul 20, 12:46 am, Sandeep G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [October 6, 2008, 9:26am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/6 "2008-10-06T09:26:52Z")

</div>

Hi Vinay,

This shud help you,  
getCommunity is the action in my controller, which gets called after  
update.  
Also notice, that i get the id of chosen value by doing value.id , also  
events are captured in element and value. Hope this helps.

\<%= text\_field\_with\_auto\_complete ‘salary\_review’,‘company’, {},  
:skip\_style =\> true ,:frequency =\> 0.1, :indicator =\>  
‘status-indicator’,  
:after\_update\_element =\> “function(element,value){” +  
remote\_function(:update =\> ‘salary\_review\_company’, :url =\> { :action =\>  
:getCommunity }, :with =\> "‘id=’+ value.id ") + “}” %\>

If the user doesn’t choose any, i tried using observe\_field. But it  
didn’t do too well, so i stopped using it.

Regards,  
Sandeep G

Vinay wrote:

> Hi Sandeep,  
> Can you show me how to call a remote\_function  
> with :after\_update\_element? I need almost the exact same  
> functionality. Once a user chooses an option from the autocomplete  
> list, i want to update a div with more information relating to the  
> selected option. Also, did you find a solution for when the user  
> doesnt select anything from the list?  
> Also, how did you manage to parse the id of the selected option along  
> with the form values? Mine is a belongs\_to association and so i am  
> using model\_auto\_completer for now. I hope you can help me with this.  
> Here is my post on this  
> [http://groups.google.co.in/group/rubyonrails-talk/browse\_thread/thread/5a91ddc41c1c44d9](http://groups.google.co.in/group/rubyonrails-talk/browse_thread/thread/5a91ddc41c1c44d9)
> 
> On Jul 20, 12:46ï¿½am, Sandeep G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [October 6, 2008, 10:46am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-trigger-on-nonselecting/143514/7 "2008-10-06T10:46:59Z")

</div>

Thanks Sandeep! will try this soon. “Events are captured in element  
and value”. I dont quite understand that. Can you please explain? I  
dont know much of JS and am quite new to ROR too.  
Ideally, I want a functionality like Gmail’s To address bar. It should  
have autocomplete, find\_or\_create\_by\_email and an extra feature of a  
live search after\_update\_element which updates a div with more info  
about the contact using the email/name entered in the field. If the  
entered string does not match any results, it should validate for an  
email address and create a contact using that info alone. I know its a  
tall ask :)… but things are falling into place and I just thought it  
would be helpful to me if you had any tips since you seem to have done  
something similar to an extent. Thanks again!

On Oct 6, 12:26 pm, Sandeep G. \<rails-mailing-l…@andreas-
