1

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);
});
21
  • 2
    try this var text = $('#copy_history').html(); Commented Sep 30, 2016 at 12:35
  • 1
    @Teemu a textarea accepts html 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. 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" 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 come 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 Commented Sep 30, 2016 at 13:42

3 Answers 3

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

Hope this will work..

Sign up to request clarification or add additional context in comments.

2 Comments

Don't put html in both. For $(.past_history) keep it as val().
ok it works but it does not save the html? i will check the anti injection, might be the problem xss
4

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.com/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.com/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>

1 Comment

To include given markup element, this would be easier: $('#input').val($('#sample').prop('outerHTML'));
0

Try append() insteal of val():

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.