PDA

View Full Version : How to blank out a text box onclick



richxyz
02-03-2009, 08:16 AM
I'm a newbie... and hopefully this is a simple thing to do. I want to put a text box on my HTML form that will have a default value of "Your Email" and when the user clicks into this text box, it will blank out the default value, allowing the user to enter his/her email address (without having to delete it first.)

Is it possible to use the onclick for this? I need a code sample to see how to do this. Thank you in advance.!

wysiwyg
02-03-2009, 08:57 AM
The event you want is onFocus, which is when the element takes focus, of course. There are several different ways to select your text box other than clicking, such as tabbing or caret navigation.

You can also use onBlur to refill the default value if the textbox is left blank, here's an example of the two in action:


<script type="text/javascript">
function blank(a) { if(a.value == a.defaultValue) a.value = ""; }
function unblank(a) { if(a.value == "") a.value = a.defaultValue; }
</script>
<input type="text" value="email goes here" onfocus="blank(this)" onblur="unblank(this)" />

richxyz
02-03-2009, 09:59 AM
It works great! Thank you very much!