admin管理员组文章数量:1426620
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 aninput
, 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
3 Answers
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);
});
本文标签: javascriptjquery copy html from div and paste into a inputStack Overflow
版权声明:本文标题:javascript - jquery copy html from div and paste into a input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745480538a2660152.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论