hubrid
hubrid

Reputation: 155

jquery why isn't this working?

$(document).ready(function()
{
    $('#comment textarea').click(function()
    {
        if (this.value == this.'Post a comment')
        {
            this.value = '';
        }
    });

    $('#comment textarea').blur(function()
    {
        if (this.value == '')
        {
            this.value = this.'Post a comment';
        }
    });
});

I have this bit of script stuck on a php file. However it is not functioning. The textarea's default value is Post a comment. This script should change the textarea's value to null on click, and then on blur change it back to post a comment.

Upvotes: -1

Views: 209

Answers (4)

ThiefMaster
ThiefMaster

Reputation: 318788

You could do it much nicer using by adding placeholder="Post a comment" to your <textarea>. While this is not supported by some older browsers it's working fine in modern browsers and does not need any scripts.

Demo: http://jsfiddle.net/ThiefMaster/vVxrp/

Upvotes: 1

kobe
kobe

Reputation: 15835

The followig code should work:

     $(document).ready(function() {
         $('#comment textarea').click(
             function() {
                 if (this.value == 'Post a comment') {
                     this.value = '';
                 }
             }
         );
         $('#comment textarea').blur(
             function() {
                 if (this.value == '') {
                     this.value = 'Post a comment';
                 }
             }
         );
    });

Upvotes: 1

alex
alex

Reputation: 490647

As zerkms said, this.'Post a comment' does not make sense.

It will give you...

Uncaught SyntaxError: Unexpected string

You probably just mean the string 'Post a comment'.

Also, this is the native DOM element, you need to wrap it with $() to make it a jQuery object.

Upvotes: 1

2hamed
2hamed

Reputation: 9067

this code i think should work(if you're using jQuery):

$(document).ready(function() {
    $('#comment textarea').click(
    function() {
    if ($(this).val() == 'Post a comment') {
    $(this).val('');
    }
    }
    );
    $('#comment textarea').blur(
    function() {
    if ($(this).val() == '') {
    $(this).val('Post a comment');
    }
    }
    );
    });

Upvotes: 0

Related Questions