Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear textarea on click?

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?

like image 734
Karem Avatar asked May 06 '10 18:05

Karem


People also ask

How do you clear a text box in HTML?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

Does textarea have Onchange?

Description. The onchange property of a Textarea element refers to an event handler function that is invoked when the user changes the value in the text area and then “commits” those changes by moving keyboard focus elsewhere.

How do I display textarea content in HTML?

Use the <textarea> tag to show a text area. The HTML <textarea> tag is used within a form to declare a textarea element - a control that allows the user to input text over multiple rows. Specifies that on page load the text area should automatically get focus.


1 Answers

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> 
like image 155
Dolph Avatar answered Sep 20 '22 01:09

Dolph