# Autocomplete 2nd field based on 1st field's input

**URL:** <https://www.ruby-forum.com/t/autocomplete-2nd-field-based-on-1st-fields-input/57415>\
**Category:** Rails\
**Created:** [April 10, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/autocomplete-2nd-field-based-on-1st-fields-input/57415 "2006-04-10T19:18:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [April 10, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/autocomplete-2nd-field-based-on-1st-fields-input/57415/1 "2006-04-10T19:18:36Z")

</div>

I’m trying to populate a field named “file\_name” based on the input of a  
field named “title”. So, for instance, based on the following input in  
the “title” field:

My Title Is Awesome

I would want the following “file\_name” to be autocompeted on the fly:

My\_Title\_Is\_Awesome

I’ve been going over all the autocomplete examples out there, but since  
they all seem to apply only to pulling data from a model, I’m not sure  
how I could approach this. Surely others have done something like this  
in rails. Any suggestions?

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [April 10, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/autocomplete-2nd-field-based-on-1st-fields-input/57415/2 "2006-04-10T19:41:42Z")

</div>

Ryan W. wrote:

> I’m trying to populate a field named “file\_name” based on the input of a  
> field named “title”. So, for instance, based on the following input in  
> the “title” field:
> 
> My Title Is Awesome
> 
> I would want the following “file\_name” to be autocompeted on the fly:
> 
> My\_Title\_Is\_Awesome
> 
> I’ve been going over all the autocomplete examples out there, but since  
> they all seem to apply only to pulling data from a model, I’m not sure  
> how I could approach this. Surely others have done something like this  
> in rails. Any suggestions?

I’m not sure of the exact syntax of the code, but in theory what you  
might want to do is use the observe\_field function in your view, to  
observe the title field. You could set observe\_field to call a method  
in your controller which uses RJS to set the innerHTML of your file\_name  
field.

Look into observe\_field, and I’m not sure exactly how you set the  
innerHTML with RJS, but that’s where I’d start.

Jeff C.man

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [April 29, 2006, 12:57am UTC](https://www.ruby-forum.com/t/autocomplete-2nd-field-based-on-1st-fields-input/57415/3 "2006-04-29T00:57:28Z")

</div>

You can try something like this:

\<%= text\_field( ‘some\_class’, ‘title’ ) %\>  
\<%= observe\_field(  
‘some\_class\_title’,  
:function =\>  
“$(‘some\_class\_file\_name’).value=value.toLowerCase().replace(/\s/g,  
‘\_’)”,  
:frequency =&mt; 0.5 ) %\>  
\<%= text\_field( ‘some\_class’, ‘file\_name’ ) %\>

This should update the “file\_name” field using the contents of “title”  
and format the string according to whatever you want. In this case, the  
“observe\_field” method will work locally without Ajax calls.

In case you need a more complex logic, the function could be executed in  
the controller. In order to update the second field you could have an  
empty DIV or something at the bottom of the page and print from the  
controller the javascript block. Should also work.

R.

---

<div class="post-metadata">

**Author:** ![jimmyether](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@jimmyether](https://www.ruby-forum.com/u/jimmyether)\
**Post date:** [April 29, 2006, 1:21am UTC](https://www.ruby-forum.com/t/autocomplete-2nd-field-based-on-1st-fields-input/57415/4 "2006-04-29T01:21:42Z")

</div>

Yes! Thanks! This did indeed work:

```
<%= text_field( 'some_class', 'title' ) %>
<%= observe_field(
  'some_class_title',
  :function => 
"$('some_class_file_name').value=value.toLowerCase().replace(/\s/g, 
'_')",
  :frequency => 0.5 ) %>
<%= text_field( 'some_class', 'file_name' ) %>
```
