I want a textarea with some default text. When the user clicks in the textarea the default text should be deleted. How can I make value of a textarea disappear on click?
I want it exactly like this, http://www.webune.com/forums/20101025cgtc.html
But I wish it made in jQuery.
<textarea id="textarea">This should be removed..</textarea>
I use this as its a bit more generic - it will clear out the element's value on focus, but return the element's value to the default value if empty.
You can accomplish this even without JavaScript by using
placeholder
attribute.But you should be aware that not every browser supports it yet. In this case you can use for instance this plugin: http://plugins.jquery.com/project/input-placeholder
This should work:
Live demo: http://jsfiddle.net/g7UKN/1/
Update:
This should do it:
Live demo: http://jsfiddle.net/g7UKN/2/
//edit
And if someone wants to do this trick on a ajax loaded textareas you can achieve this with the
.live()
event ;)You need two handlers, one for when the element gets focus and one for when it loses it. When it gets focus, check to see if the value is only space characters and, if so, set the value to the default.