nasty
nasty

Reputation: 7077

jQuery Clone Div content into Textarea

I want to clone div content to textarea. It works fine from Div to Div, but not from Div to Textarea. I have no idea what Im missing. Can you guys please help me on this?

Current jQuery

$('.policyCopyBtn').click(function(){
    $('.leftPart').clone().appendTo('.policyDetails');
});

Current HTML

<div class="leftPart">Some text here</div>
<div class="rightPart"><textarea class="policyDetails"></textarea></div>
<input type="submit" class="policyCopyBtn" />

JS FIDDLE : https://jsfiddle.net/c2fny59x/

I cant use .html() because this will copy all my formatting in the DIV as well. I just want plain text copied across.

Upvotes: 1

Views: 1906

Answers (4)

Vijay Rathore
Vijay Rathore

Reputation: 603

Replace all the tags with "".

 $('.policyCopyBtn').click(function() {
  $('.policyDetails').val($('.leftPart').html().replace(/<[^>]+>/ig,""));
 });

Check the working fiddle https://jsfiddle.net/c2fny59x/8/ . I hope this helps.

Upvotes: 0

bellabelle
bellabelle

Reputation: 926

Simply, in your JS append the .leftPart text contents to .policyDetails textarea. Here's the working jsfiddle: https://jsfiddle.net/x346my5j/

$('.policyCopyBtn').click(function() {
   $('.policyDetails').append($('.leftPart').text());
 });

Upvotes: 1

Rohan Fating
Rohan Fating

Reputation: 2133

Use val() function to set value of text area, text() function gives inner text.

<div class="leftPart"><p>
Text from paragraph
</p>Some text here</div>
<div class="rightPart">
  <textarea id="textA"class="policyDetails"></textarea>
</div>
<input type="submit" class="policyCopyBtn" />


  $('.policyCopyBtn').click(function() {
   $('#textA').val($('.leftPart').text());
 });

Upvotes: 1

MD. Khairul Basar
MD. Khairul Basar

Reputation: 5110

You need to get the text using text() and set the text using val().

$('.policyCopyBtn').click(function() {
   var text = $('.leftPart').text();
   $('textarea').val(text);
});

Upvotes: 2

Related Questions