最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery copy html from div and paste into a input - Stack Overflow

programmeradmin0浏览0评论

i am trying to copy the raw html with a div called #copy_history. I have managed to do it with the text works fine but i require the html as well.

The following does work but its grabbing the text not all the html:

script:

$('#copy').click(function() {
    var text = $('#copy_history').text();
    $('.paste_history').val(text);
});

i am trying to copy the raw html with a div called #copy_history. I have managed to do it with the text works fine but i require the html as well.

The following does work but its grabbing the text not all the html:

script:

$('#copy').click(function() {
    var text = $('#copy_history').text();
    $('.paste_history').val(text);
});
Share Improve this question asked Sep 30, 2016 at 12:33 alwayslearningalwayslearning 4115 silver badges18 bronze badges 21
  • 2 try this var text = $('#copy_history').html(); – shubham715 Commented Sep 30, 2016 at 12:35
  • 1 @Teemu a textarea accepts html – Pete Commented Sep 30, 2016 at 12:37
  • 2 @Teemu Any input that accepts text can accept markup... You cannot render HTML inside an input, but you may enter any text you want. – André Dion Commented Sep 30, 2016 at 12:41
  • 1 "but it is not saving html*" - there's nothing in the question about "saving" - please ask a new question and mark one of the answers as correct for the problem as described in the question - do not add-on extra, hidden, new "but now it does Y" – fdomn-m Commented Sep 30, 2016 at 13:16
  • 2 It's not "nonsense" - you ask a question, you get (multiple) answers that answer that question. If you then change that question, by adding "but now...", it's a different question and the existing answers, that people spent their time providing for you, now no longer answer the new question and what happens is other people e to the question and read the new question and see the answers for the old question and go "that doesn't answer the [new] question", not realising it's new and downvote the answers. Which is unfair and frankly, a bit rude to those that spent their time helping you – fdomn-m Commented Sep 30, 2016 at 13:42
 |  Show 16 more ments

3 Answers 3

Reset to default 4
$('#copy').click(function() {
    var text = $('#copy_history').html();
    $('.paste_history').val(text);
});

Hope this will work..

See html and/or clone:

Grab the markup inside a given element:

$('#copy').on('click', function() {
  $('#input').val($('#sample').html());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="sample">Here's some <b>markup</b></div>

<label for="input">Input</label>
<input type="text" id="input"/>

<button id="copy">Copy sample markup</button>

Grab the markup inside and including the given element:

$('#copy').on('click', function() {
  $('#input').val($('<div/>').append($('#sample').clone()).html());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="sample">Here's some <b>markup</b></div>

<label for="input">Input</label>
<input type="text" id="input"/>

<button id="copy">Copy sample markup</button>

Try append() insteal of val():

$('#copy').click(function() {
    var text = $('#copy_history');
    $('.paste_history').append(text);
});
发布评论

评论列表(0)

  1. 暂无评论