views:

640

answers:

3

I am making a simple form with about eight input fields. I want the initial value in the field to indicate what people are supposed to enter. For example value="name" so that people know to enter there name there.

My question though is how do you get the initial value to vanish when they enter that field so that the initial value is only a hint and not a permanent value that they have to erase before they can type in their name?

Thanks!

+3  A: 

Set the 'default' value to "Name". Then, using javascript and the 'onfocus' event, clear the field.

So you would have:

<input type="textbox" value="Name" onfocus="if (this.value=='Name') this.value='';"/>
Erich
Gah. You answered 30 seconds before me :(
Daniel S
Works just right. Thank you for the quick responses!
fmz
Remember to select an answer as "accepted" by clicking on the green tick under the votes.
Daniel S
+1  A: 

Basic javascript:

<input type="text" value="name" onfocus='if(this.value=="name"){this.value="";}' />
Daniel S
A: 

This is how you should do it.

<input type="text" value="name" onfocus="this.value = this.value=='name'?'':this.value;" onblur="this.value = this.value==''?'name':this.value;">

If they click on it and click off the default value will go back. If they click on it and type something in, it won't try to erase what they put if they go back to click on it again.

John