jz3
jz3

Reputation: 521

jquery clearing textarea

I'm not sure why I am having this issue, if you can help I would appreciate it.

I wrote a function to clear a textbox on click, but it isnt working correctly:

$(function() 
{
    $('textarea#contact_t').click( function() 
    {
        if ($(this).val() == 'Enter your message Here!') 
        {   
            $(this).val('');    
        }
    });
});

The problem is that it will not allow me to write a value of '' to the textarea. This same function works if I change the statement inside the if to $(this).val(' ') (with a space in between the quotes). But I want the textbox to be cleared, not have a space at the beginning. It isn't letting me write an empty string to the textarea.

Upvotes: 0

Views: 4636

Answers (3)

Hogan
Hogan

Reputation: 70538

Try using this code... I bet it solves your problem...

$(function() {
    $('textarea#contact_t').click( function() {

         if ($(this).val().toUpperCase() == 'ENTER YOUR MESSAGE HERE!') {  
            $(this).val('');  
         }

    });
  }
);

Upvotes: 0

No Results Found
No Results Found

Reputation: 102864

If you can live without supporting older browsers, just drop the jquery and switch to the HTML5 placeholder attribute:

<textarea placeholder="Enter your message here"></textarea>

Demo: http://jsfiddle.net/UeuaW/

Upvotes: 0

Alex Peattie
Alex Peattie

Reputation: 27697

This does what (I think) you want (and is a little cleaner):

$('textarea#contact_t').one("focus", function() {
  $(this).val('');
});

Upvotes: 2

Related Questions