# Textfield\_with\_auto\_complete - filling two fields

**URL:** <https://www.ruby-forum.com/t/textfield-with-auto-complete-filling-two-fields/62109>\
**Category:** Rails\
**Created:** [May 31, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/textfield-with-auto-complete-filling-two-fields/62109 "2006-05-31T16:45:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 31, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/textfield-with-auto-complete-filling-two-fields/62109/1 "2006-05-31T16:45:22Z")

</div>

I am going nuts here…

I have a form where users can write a city name (used with  
auto\_complete) - and once they click on the desired city in the dropdown  
menu - I would like to have the Country field filled automatically as  
well…

Is this possible at all ?  
If so, can somebody please tell me where I can find some documentation  
on the subject, or preferrably an example ?

Many thanks in advance,

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [May 31, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/textfield-with-auto-complete-filling-two-fields/62109/2 "2006-05-31T19:12:08Z")

</div>

I’ve been trying to do this too. What I have found  
[http://rubyonrails.org/api/classes/ActionView/Helpers/JavaScriptMacrosHelper.html](http://rubyonrails.org/api/classes/ActionView/Helpers/JavaScriptMacrosHelper.html)  
it looks like you might need auto\_complete\_field. I haven’t had time  
to work with it but it looks like you can call a JS function after a  
selection is made.( :after\_update\_element:)  
I don’t think that function works with text\_field\_with\_auto\_complete.

hth

john.  
if you get it to work let me know.

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [June 4, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/textfield-with-auto-complete-filling-two-fields/62109/3 "2006-06-04T18:07:48Z")

</div>

mich wrote:

> In my view:  
> \<%= text\_field\_with\_auto\_complete :project, :code, {},  
> :after\_update\_element =\>  
> “function (field, element) {  
> var my\_array = field.value.split(’-’);  
> field.value = my\_array[0];  
> document.getElementById(‘project\_client\_name’).value =  
> my\_array[1];  
> }” %\>

Actually, I had to add some code to remove newlines (\n) as it was added  
to each field, it looks like this now:

\<%= text\_field\_with\_auto\_complete :project, :code, {},  
:after\_update\_element =\>  
“function (field, element) {  
var my\_array = field.value.split(’-’);  
field.value = my\_array[0];  
field.value = field.value.replace(/^\s\*|\s\*$/g, ‘’);  
field.value = field.value.replace(/\n/, ‘’);  
client\_name = my\_array[1].replace(/\n/, ‘’);  
document.getElementById(‘project\_client\_name’).value =  
client\_name;  
}”, %\>

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [June 4, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/textfield-with-auto-complete-filling-two-fields/62109/4 "2006-06-04T17:09:47Z")

</div>

John I. wrote:

> I’ve been trying to do this too. What I have found  
> [http://rubyonrails.org/api/classes/ActionView/Helpers/JavaScriptMacrosHelper.html](http://rubyonrails.org/api/classes/ActionView/Helpers/JavaScriptMacrosHelper.html)  
> it looks like you might need auto\_complete\_field. I haven’t had time  
> to work with it but it looks like you can call a JS function after a  
> selection is made.( :after\_update\_element:)  
> I don’t think that function works with text\_field\_with\_auto\_complete.

After a whole lot of head-scratching I managed to get it work !

Here’s what I’ve got:

auto\_complete template:

\<% for company in @companies do -%\>- 
\<%= h(company.code) %\>-\<%= h(company.name) %\>
\<% end %\>

In my view:  
\<%= text\_field\_with\_auto\_complete :project, :code, {},  
:after\_update\_element =\>  
“function (field, element) {  
var my\_array = field.value.split(‘-’);  
field.value = my\_array[0];  
document.getElementById(‘project\_client\_name’).value =  
my\_array[1];  
}” %\>

I’m using hyphen(-) as a delimiter, but you can use whatever you want.

There’s without a doubt a smarter way of doing it, but this seems to  
work alright for me. I’ve tested it with firefox and opera… I’d love to  
hear if it works with IE as well !

/mich
