Set value of textarea in jQuery

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?


Solution 1:

Have you tried val?

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

Solution 2:

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 :)

Solution 3:

$("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.

Solution 4:

Oohh come on boys! it works just with

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

Solution 5:

I think this should work :

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