user1576449
user1576449

Reputation: 11

Using jQuery .val() to set the contents of a div

In my HTML:

< div id="textarea" style="overflow: scroll;"> < /div>

I'm trying to put a piece of javascript text which is set in var X into the textarea but whenever I do

$("#textarea").val(X);

nothing happens, whereas if I do

$("#textarea").text(X);

the text version of X shows up but not the formatting like \n.

How can I get X to show up properly in my textarea?

Upvotes: 1

Views: 193

Answers (3)

Ram
Ram

Reputation: 144689

val() is used for setting/getting the value of textarea and input elements, for div elements you should use text() or html(). If you want to add HTML markup you should use html() method instead:

Get/Set the HTML contents of the first element in the set of matched elements.

$("#textarea").html(X);

Upvotes: 3

bugwheels94
bugwheels94

Reputation: 31920

use .html()

$("#textarea").html(X);

Upvotes: 1

codeandcloud
codeandcloud

Reputation: 55210

Naming a div textarea won't make it a textarea. Try this.

$("#textarea").html(X);

val is for input, textarea etc...

Upvotes: 2

Related Questions