scotsqueakscoot
scotsqueakscoot

Reputation: 258

textarea.val() setting and getting

initially, my textarea has a value of Enter Comment... set via <textarea>Enter Comment...</textarea>

I have a .focus like:

$("textarea.commentTextArea").live('focus', function() { if($(this).val() == "Enter Comment...") { $(this).val(''); } });

and a .blur() like:

$("textarea.commentTextArea").live('blur', function() { if($(this).val() == "Enter Comment..." || $(this).val() == "") { $(this).text("Enter Comment..."); $(this).parent().addClass("hide"); $(this).parent().hide(); } });

However, after I set the val in the .blur() function, and try to retrieve the value using .val(), it's always returning 'Enter Comment...' as opposed to the text that was user inputted. I'm at a loss here.

Thanks for the help!

Edit- I have a more in depth selector for the "textarea", that's not the issue. It wouldn't be returning "Enter Comment" if the selector was returning null. Thanks for trying though. Same goes with my .focus() function. I just wanted to show the core of what's going on

Upvotes: 1

Views: 1160

Answers (3)

Marko
Marko

Reputation: 72230

I presume it's because you missed the quotes around the $(textarea).blur() method.

I've created an example with your code which works.

Upvotes: 0

jwheron
jwheron

Reputation: 2572

My first guess would be that $(textarea) is returning null -- I'm guessing that it should be $("textarea"). I think this is fine. I tried it out using jsfiddle, and it worked.

Upvotes: 0

Ray
Ray

Reputation: 21905

You don't have quotes around the word 'textarea' in the second statement. Also, your focus event will delete anything the user entered if they return to the textarea to edit again.

do this to solve the second problem (same trick you used on the blur event):

$("textarea").focus(function() { if ($(this).val() == 'Enter Comment...') $(this).val(''); });

Upvotes: 2

Related Questions