View Full Version : Making a Initial Text Value Vanish one selected
01-11-2008, 12:47 PM
<input type="text" name="name" size="20" maxlength="30" value="Name"/>
on clicking the above field how can i make the value Name vanish ??
01-11-2008, 01:27 PM
<input type="text" name="name" size="20" maxlength="30" value="Name" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" />
01-11-2008, 03:35 PM
10-04-2008, 01:57 AM
Thank you Thank you Thank you
You are the best. Nice, clean code too - works with other events perfectly =]
10-04-2008, 02:04 AM
Just out of interest...
Say there were multiple text inputs with different values.
Would you need a set of the code for each one or is the a var option for that, like 'get the value'?
10-04-2008, 02:42 AM
oops replied in the wrong thread - my bad
ill repost in the correct one =[
12-31-2008, 02:33 PM
This code is a bit better because they can now enter in as input whatever the default text is (this can come in handy if you're giving a default date or first name or whatever):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
input_element.value = "";
input_element.alreadyCleared = true;
<form onsubmit="return false">
<input type="text" onfocus="clearOnce(this)" value="type stuff here" /><br />
<input type="text" onfocus="clearOnce(this)" value="and here" />
12-31-2008, 02:58 PM
This code is a bit better because they can now enter in as input whatever the default text is (this can come in handy if you're giving a default date or first name or whatever): but that won't retain the original text when the user deletes everything s/he typed.
12-31-2008, 04:46 PM
I wasn't commenting on the given onblur - that one was fine, although modularizing it into a central function would be preferable.
The onblur function to me seems a bit more case-by-case-ish anyway. In most of the forms I create, I wouldn't want the default value to pop back in if the user deleted the input text. Whether you want it to do that or not is up to the developer.
11-01-2009, 02:11 PM
Thanks, Thats great!!!
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.