0

How I can change my textarea content on click to be empty?

Here is the code:

http://jsfiddle.net/QMfyP/

<textarea name="adventage" style="width:400px; border-radius: 3px; border-left: 4px solid #6C3" id="adventage" cols="45" rows="5">Share with everyone what are the advantages of this vehicle. </textarea>

The idea is on click to this area, it will be empty for typing new text.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Hued Bafuli
  • 107
  • 2
  • 10

6 Answers6

4

You can get that functionality using the placeholder attribute

<textarea placeholder='Share with everyone what are the advantages of this vehicle.'></textarea>

It is the standard and easiest way (no javascript involved, you can see it working in stackoverflow's search textbox in the top of this page!).

It will not work in old browsers though (IE9 and older), if you need it to work in those browsers you can check this question

Community
  • 1
  • 1
jorgehmv
  • 3,633
  • 3
  • 25
  • 39
  • 1
    Good suggestion, but 'placeholder' isn't supported in IE 9 and earlier versions. If browser compatibility isn't an issue for you, this is perfect, otherwise use @adamb answer. – Alex Mar 18 '13 at 14:43
1

Give the textarea a unique id (as you probably won't want to apply this to all textareas), e.g. thetextarea:

$("#thetextarea").click(function(){
   $("#thetextarea").empty();
});

Here's a working jsFiddle.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
0
$('YOURSELECTOR').click(function(e) {
    $('#adventage').empty();
});
adamb
  • 4,815
  • 1
  • 25
  • 40
0

http://jsfiddle.net/QMfyP/6/

HTML

<textarea class="my_textarea" name="adventage" id="adventage" cols="45" rows="5">Share with everyone what are the advantages of this vehicle. </textarea>

JQUERY

$("#adventage").click(function() {
  $(this).empty();
});

CSS

#adventage { width:400px; border-radius: 3px; border-left: 4px solid #6C3; }
Lowkase
  • 5,631
  • 2
  • 30
  • 48
0
$(document).ready(function(){
    $("textarea[name='adventage']").click(function({
        $(this).innerHTML("");
    }))
})

This should work!

0

try like this.

For the first time place holder works.But if you have sometext already inside that (in case of editing) do some scripting.

$(document).ready(function(){
        $('#adventage').click(function(){
            $(this).val('');
        })

});
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307