appendTo working with div but not with textarea

143 Views Asked by At

I have the following jQuery code running:

$('a.testlink').click(function() {
    var $essay = $(".tinyeditor iframe").contents().find("body").clone();
    $essay.appendTo("div.test");
});

As you can see, it is finding the contents of "body" inside an iframe (same source); and appending it to a div.

The above code works fine. But if I change the appendTo from a div to a textarea, it doesn't work.

What am I doing wrong?

3

There are 3 best solutions below

1
Milind Anantwar On BEST ANSWER

.appendTo() appends the content as an html. it will not work for textarea. you need to set the value for textarea.

For appending inside textarea, you need to set the textarea value to its original contents + new content.

 $('textarea').val($('textarea').val() + $essay.html());

Update: What if I want all the contents to be replaced with the new one

You need to simply set the value using .val() along with new content:

$('textarea').val( $essay.html());
0
James Hibbard On

You cannot append anything to a textarea.

You can however, set its value using the .val() method.

$('textarea').val(whatever);
0
Rory McCrossan On

You need to set the val() of the textarea, not append to it:

$('textarea.test').val($essay.html());