jQuery clone()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery clone()

The jQuery clone() method is used to make copies of the set of matched elements. It also makes copies of their child nodes, texts and attributes. The clone() method is a convenient way to duplicate elements on a page.

Syntax:

  1. $(selector).clone(true|false)  

Parameters of jQuery clone() method

Parameter Description
True It specifies that event handlers also should be copied.
False It is a default parameter. It specifies that event handler should not be copied.

Example of jQuery clone() method

Let's take an example to demonstrate the effect of jQuery clone() method.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("button").click(function(){  
  8.         $("p").clone().appendTo("body");  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <p><b> javatpoint.com is a popular tutorial website.</b></p>  
  15. <p><b>javatpoint.com is a training institute also.</b></p>  
  16. <button>Click here, to clone all p elements, and append them to the body element</button>  
  17. </body>  
  18. </html>  

Test it Now