# Login Password text field values

**URL:** <https://www.ruby-forum.com/t/login-password-text-field-values/100656>\
**Category:** Rails\
**Created:** [July 31, 2006, 5:33am UTC](https://www.ruby-forum.com/t/login-password-text-field-values/100656 "2006-07-31T05:33:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [July 31, 2006, 5:33am UTC](https://www.ruby-forum.com/t/login-password-text-field-values/100656/1 "2006-07-31T05:33:09Z")

</div>

I have a text field that I use for logging in users. Due to the design  
of my site, I don’t have room to put the word “username” in front of the  
text field so I’d like to actually display it inside the field and then  
have the word clear when the user clicks on it.

How could I modify something like this

\<%= text\_field “user”, “login”, :size =\> 15 %\>

to do that?

Thanks…

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [July 31, 2006, 5:56am UTC](https://www.ruby-forum.com/t/login-password-text-field-values/100656/2 "2006-07-31T05:56:02Z")

</div>

Vince W. wrote:

> Thanks…

Hi Vince,

I think you need to use ‘:value=\>…’ for this:  
\<%= text\_field “user”, “login”, :value =\> ‘username’, :size =\> 15 %\>

Hope this helps.  
Cheers  
mohit.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [July 31, 2006, 6:05am UTC](https://www.ruby-forum.com/t/login-password-text-field-values/100656/3 "2006-07-31T06:05:13Z")

</div>

On 7/31/06, Mohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Hope this helps.  
> Cheers  
> mohit.
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

The value option is definitely required. Also if you want it to go  
blank  
when the user clicks in it then you will need some JS. You will need to  
give your username text\_field an HTML id parameter so you can reference  
it  
in the JS. Somthing like

function clear\_usernamed( dom\_id ) {  
if $( dom\_id ).value == “username” {  
$( dom\_id ).value = “”;  
}  
}

and your field

\<%= text\_field “user”, “login”, :value =\> ‘username’, :size =\> 15,  
:onFocus  
=\> "clear\_username( ‘user\_login’);’ %\>

Then call this on the onFocus action for your username field. I’m not  
exactly sure of the format for onFocus. :on\_focus, :onFocus, ‘onFocus’  
Unless someone else can help you’ll need to experiment.

If you really want to make it really nice you could include this in a  
behaviour, but the basic case above should work.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [July 31, 2006, 6:06am UTC](https://www.ruby-forum.com/t/login-password-text-field-values/100656/4 "2006-07-31T06:06:19Z")

</div>

Mohit S. wrote:

> I think you need to use ‘:value=\>…’ for this:  
> \<%= text\_field “user”, “login”, :value =\> ‘username’, :size =\> 15 %\>

Hi Mohit,  
It gets me 50% of the way there… all that’s left is clearing the field  
when it’s selected (so people don’t have to manually erase ‘username’ to  
enter their user name.

Is there any equivalent of an onfocus clear or anything for rails?

-Vince

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [July 31, 2006, 6:12am UTC](https://www.ruby-forum.com/t/login-password-text-field-values/100656/5 "2006-07-31T06:12:03Z")

</div>

Bingo!

Here is the final text field:

```
<%= text_field "user", "login", :value => 'username', :size => 15 

```

,:class =\>‘loginbox’, :onFocus =\> ‘clearDefault(this)’ %\>

and the corresponding javascript:

Thanks guys!
