top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create clone of any object using jQuery?

+3 votes
348 views
How to create clone of any object using jQuery?
posted Jul 14, 2015 by Shivaranjini

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

jQuery provides clone() method which creates deep copy set of all the elements which means it will copy the child nodes also. Clone() method is the best way to duplicate content of any element. For example, I have placed a div and a button. On click of button, we will clone the div and add it to body tag.

<div id="dvText">
   jQuery By Example Rocks!!!
</div>
<input type="button" id="btnClone" value="Clone Div" />
jQuery Clone() method code. 
$(document).ready(function(){
  $('#btnClone').click(function(){
     $('#dvText').clone().appendTo('body');
     return false;
  });
});

As you see, I have just called the clone() method on the element which you want to clone and added it to body.

answer Jul 14, 2015 by Karthick.c
...