Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Is it possible copy and paste HTML?

Tags:

jquery

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

like image 213
markzzz Avatar asked Apr 14 '11 23:04

markzzz


2 Answers

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/

like image 67
Town Avatar answered Oct 21 '22 03:10

Town


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');
});
like image 28
Ady Ngom Avatar answered Oct 21 '22 04:10

Ady Ngom