简体   繁体   中英

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.

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

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

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());
 });

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());
 });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM