views:

811

answers:

3

Given a <textarea> with a default value as follows:

<textarea>Please describe why</textarea>

How do you clear the default value when the user clicks to edit the field?

A: 
<textarea onClick="javascript: this.value='';">Please describe why</textarea>
Why minus 1, it does what the poster asked.
The downvote isn't mine, but this solution wipes out the text field *every time* the user clicks on it.
Pekka
+4  A: 

Your JavaScript:

function clearContents(element) {
  element.value = '';
}

And your HTML:

<textarea onfocus="clearContents(this);">Please describe why</textarea>

I assume you'll want to make this a little more robust, so as to not wipe user input when focusing a second time. Here are five related discussions & articles.

And here's the (much better) idea that David Dorward refers to in comments above:

<label for="explanation">Please describe why</label>
<textarea name="explanation" id="explanation"></textarea>
Dolph
The for attribute needs to match an id, not a name! (Otherwise it would be incompatible with radio buttons)
David Dorward
+1 Great work, a great answer.
Pekka
@David Dorward, you are very correct, sir! Fixed.
Dolph
You should get rid of the `javascript:` bit. It is a label with no goto. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/label If you use intrinsic event attributes then you are required to specify the language used, but this is achieved with a meta tag, not a label: http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.2.1
David Dorward
@David Dorward, correct again! Thanks for the links - I've never read that part of the spec.
Dolph
+2  A: 

Try:

<input name="mytextbox" onfocus="if (this.value=='Please describe why') this.value = ''" type="text" value="Please Describe why">
Paul Kearney - pk
This is not a textarea, so -1. This does only delete the sample text, so +1.
Andreas Rejbrand