alwayslearning
alwayslearning

Reputation: 411

jquery copy html from div and paste into a input

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

Upvotes: 1

Views: 2384

Answers (3)

Rax Weber
Rax Weber

Reputation: 3780

Try append() insteal of val():

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

Upvotes: 0

André Dion
André Dion

Reputation: 21708

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>

Upvotes: 4

Bhagwat K
Bhagwat K

Reputation: 3142

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

Hope this will work..

Upvotes: 4

Related Questions