<input type="text" value="Your value" onfocus="this.value=''" />
How can I make that when a user clicks in a form field, the value I put in get's erased quickly, without heim having to select and delete it?
![]() | Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years. Thanks, CNET Support |
How can I make that when a user clicks in a form field, the value I put in get's erased quickly, without heim having to select and delete it?
Discussion is locked
This would be truly ODD BALL behaviour for a web form. Doing this would make that page behave different from all other web forms I've used.
DON'T DO THIS.
If you want to go from field to field and auto-highlight the content, tap the TAB key.
Bob
Without going further into creating a JavaScript function, the easiest way would be to:
<input type="text" name="fieldname" value="blah blah" onfocus="this.value=''; return true;" />
The only problem with this is that whenever you return to this field, the data gets erase.
Now, if you want to add a little JavaScript check before it erases the data...
<script language="javascript" type="text/javascript">
<!--
function checkClear(f)
{
if ( f.value == "blah blah" ) return ''
else return f.value
}
//-->
</script>
then later in the form, you have:
<input type="text" name="fieldname" value="blah blah" onfocus="this.value=checkClear(this); return true;" />
So if the user returns to this particular field and the initial value of "blah blah" is changed, it won't erase it.