Javascript clear text area on mouse click
To clear text from a pre-seeded textarea or input text on a mouse click, you have to use some javascript. Luckily, it’s very simple to implement this bit of javascript. This short tutorial will show you how and explain what the code used means.
Example (click on the textarea and the pre-seeded text “This text will disappear when you click the textarea with the mouse” will be cleared):
The codes used for this is as follows
<form name="myform">
<textarea name="mytext" cols="30" rows="5"
onclick="document.myform.mytext.value='';">
This text will disappear when you click the textarea with the mouse</textarea>
</form>
The first line opens a html form tag with the name “myform”. You will need to name the form as you will be referencing this form using javascript, an object orientated programming language. The name of the form is not important, you can call it whatever you like, but it does have to be a unique name.
The second line opens a textarea with the name “mytext’. Again, the name of the textarea is not important as long as it’s unique. The second line also sets the size of the textarea, in this case, it has 30 columns and 5 rows.
The third line is the javascript that clears the textarea when the textarea is selected by the mouse pointer. “onclick” is a preset javascript event that does exactly what it’s named. On a mouse click, do something. Because javascript is object orientated, you can actually get another object, such as a different form, to react when the textarea is clicked, for now, we want to manipulate the textarea we clicked, so we have to reference it.
document.myform.mytext.value='’ means in this document, in the object “myform” and within that object, look for the object “mytext” and then look for the attribute “value”, then set this value to ‘’ (null or nothing).
The fourth line is the pre-seeded message that shows up when the page loads. After you click the textarea, this message will disappear. The textarea tag is also closed on this line.
The fifth line closes the form tag.


March 8th, 2007 at 2:23 pm
good
April 25th, 2007 at 4:32 pm
yeah, good
But onblur how can you put initial text back if the user doesn’t input anything?
May 11th, 2008 at 3:44 pm
thanks, this helped me out in a project I am working on.
February 3rd, 2009 at 4:21 am
Improved it a little… hope it will work for you too…
This text will disappear when you click the textarea with the mouse
February 3rd, 2009 at 4:26 am
html didn’t work, sorry…. trying to publish somewhere else later on….
February 27th, 2009 at 4:32 am
thanks a lot that helps a lot
April 1st, 2009 at 12:22 am
Thank i was look for some code like this.
Only problem i can see is that if the user types in some text and then clicks in the box again their text is deleted!
July 27th, 2009 at 8:10 am
It’s really help..good job..thank a lots!
August 27th, 2009 at 1:55 pm
You might also want to write the code differently to handle when the user clicks the textarea again, clearing their input (annoying mistake).
This also answers bahodir’s question:
This text will disappear when you click the textarea with the mouse