tags:

views:

37384

answers:

12

I am attempting to set a value in a textarea field using jquery with the following code:

$("textarea#ExampleMessage").attr("value", result.exampleMessage);

The issue is, once this code executes, it is not altering the text in the textarea?

However when performing an alert($("textarea#ExampleMessage").attr("value")) the newly set value is returned?

+1  A: 

I think that since a <textarea> has the value stored in the content, you can't set the value attribute, and you have to instead use:

$("textarea#ExampleMessage").text(result.exampleMessage);

If that doesn't work, I'd check that your selector is actually returning something.

Ben Alpert
I agree with you, text seems like the most appropriate choice.My selector? result.exampleMessage? Yes it does, as in my alert on the next line displays the new contents, which was updated with result.exampleMessage.
GONeale
Just for the record - I tried something similar with text() in an onclick handler, and though it worked on the first click, subsequent clicks did nothing. However, val() works fine, all the time.
Matt Van Horn
+49  A: 

Have you tried val?

$("textarea#ExampleMessage").val(result.exampleMessage);
enobrev
I think that does the same as attr("value");
Ben Alpert
mostly true, although val does take the time to make sure the value you're setting is a string, and attr seems to do a bit more magic overall.
enobrev
Sorry about the muck around, val() is working. I had tried a different field, and did not re-update the textarea id ref in question.
GONeale
Yeah. A textarea doesn't have a value attribute, while an input does.
MDCore
Agreed, but val() is setting it in this instance. They [jquery] must determine it's textarea type and set the text.
GONeale
textarea has a value (JavaScript) property just like a text input. It doesn't have an HTML `value` attribute, but that doesn't matter, because both `val()` and `attr('value')` write to the JavaScript value property. Note, `attr()` does *not* set HTML attributes! It only sets JavaScript properties, whilst trying to hide the difference for cases like class/className where the name is different. But you will still see the difference for places where the attribute and property do different things, such as in form elements.
bobince
Hi......Please vote down ;) .val() in jquery just a magic. You should notice that textarea tag using inner html to display and not in value attribute just like input tag. See my answer below.
CallMeLaNN
A: 

Yep tried both. This is just nuts. attr() and val() should both work, I'm sure of it.

GONeale
have you tried alerting (or better yet, console.log-ing) result.exampleMessage before setting it to make sure it has the value you think it does before putting it into the textarea? Do you have any events that might be replacing the text? Any hovers?
enobrev
Don't respond to answers with a new answer, use the comments
John Sheehan
That was directed to GONeale
John Sheehan
i figured, but thanks for the clarification
enobrev
No problem John, I usually do, just wanted two parties to know I tried their methods.
GONeale
+5  A: 

I think this should work :

$("textarea#ExampleMessage").val(result.exampleMessage);
Jomit
+4  A: 

Textarea has no value attribute, its value comes between tags, i.e: <textarea>my text</textarea>, it is not like the input field (<input value="my text" />). That's why attr doesn't work :)

+1  A: 

It works for me.... I have built a face book wall...

Here is the basis of my code:

// SETS MY TEXT AREA TO EMPTY (NO VALUE) $('textarea#message_wall').val('');

Yep dude already answered. Welcome to Stack Overflow, the answer accepted is in green.
GONeale
A: 

Oohh come on boys! it works just with $('#your_textarea_id').val('some_value');

Mik
A: 

I had an issue with using .text('somevalue'); in firefox 3.07, 3.15. Interestingly it worked fine in Firefox 3.5.* I know thats specific but it didnt pass my companies QA testing because of this.

However - I used .val() as this worked in all versions. Just for your info.

Matt Christian
A: 

I had same issue and this solution didn't work but what worked was to use html

$('#your_textarea_id').html('some_value');

dave
A: 

I tried with .val() .text() .html() and had some bugs using jQuery to read or set value of a textarea... i endup using native js

$('#message').blur( function() {

if (this.value == '') {
  this.value = msg_greeting;
}

});

Antony
A: 

using $("textarea#ExampleMessage").html('whatever you want to put here'); can be a good way, because .val() can have problems when you are using data from database.

example:

a database field named as description has the following value "asjkdfklasdjf

sjklñadf" , using .val() to assign value to textarea can be a tedious job.

A: 

Hi...........

$("textarea#ExampleMessage").val() in jquery just a magic.

You should notice that textarea tag using inner html to display and not in value attribute just like input tag.

<textarea>blah blah</textarea>
<input type="text" value="blah blah"/>

You should use

$("textarea#ExampleMessage").html(result.exampleMessage)

or

$("textarea#ExampleMessage").text(result.exampleMessage)

depend on if you want to display it as html tags or plain text.

CallMeLaNN
You are correct, textarea doens't have a val or value, and jquery is just doing some magic to make life easier for the developer.
Scott Kirkwood
It's not called magic, it's called abstraction. textarea is only 1's and 0's, but all the layers of abstraction in your computer hides that for you. It's okay to point it out, but please don't ask people to downvote other answers for such a reason... :)
Espenhh