tags:

views:

83

answers:

3

All,

I have a text area field in my for,.How do i clear contents of it on sum onclick using jquery

Thanks.

+3  A: 
$("#textArea").click(function(){ $(this).attr({ value: '' }); });

With a couple of caveats:

  • it'll clear whenever someone clicks on it, even if they've entered data already
  • it'll only work if someone clicks on the field
mopoke
Alternatively, `$(this).val("")`.
August Lilleaas
`$(this).attr({ value: '' });` can also be expressed as `$(this).val('')`.
Tatu Ulmanen
+2  A: 
$('textarea').focus(function() {
  $(this).val('');
});

This will clear the textarea on focus (using mouse click or keyboard).

David
+1  A: 

This will clear the default value onclick and restore back onblur.

$('textarea').click(function () {
    if (this.value == this.defaultValue) {
        this.value = '';
    }
});
$('textarea').blur(function () {
    if (this.value === '') {
        this.value = this.defaultValue;
    }
});
Nimbuz