jQuery - Is it possible copy and paste HTML?
Starting with an example, if I have this lines of HTML :
<div id="divToMove">
... somethings like 100 lines of code ...
</div>
i'd like to know if I can take this div and copy and paste many times...
I tried to put a jQuery/JS function that "add" this code from javascript to the HTML page, but seems that is too slower as process. Maybe a copy and paste (if possible) is faster... Some helps? Thanks
Something like this?
HTML
<input type="button" id="copy" value=" copy "/>
<div id="content">
<span>here's a span</span>
<div>and a div</div>
</div>
jQuery
$(function() {
$('#copy').click(function(){
var content = $('#content').html();
var newdiv = $("<div>");
newdiv.html(content);
$('#content').after(newdiv);
});
});
In action: http://jsfiddle.net/Town/5q2CK/
look into .clone() and you could do things like after clicking on the target div:
$('#targetDiv').click(function(){
var cloned = $(this).clone();
// now you could put at the end another div
// $('#myOtherDiv').append(cloned);
// or put insert after another div
// $(cloned).insertAfter('#myOtherDiv');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With