Tuesday, February 17, 2009

JavaScript Clear Textfield Default Value onClick

So basically if you have a text field with default text like “Enter Email Address Here” and you want it so that when the user clicks in it, the text disappears, this is the code to use. It goes one step further so that if you do not enter anything it puts the default text back and if there is something entered it doesn’t get tripped up and clear it out. Plus it is super simple and clean and tested in all browsers.

Here is the code:

function clearText(field){

if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;

}

Here is how to use it:

< name="emailaddress" type="text" value="Enter Your Email Address" onFocus="clearText(this)" onBlur="clearText(this)">




No comments: