jQuery clone()

The jQuery clone() method is used to duplicate elements on a page, to make copies of the selected elements.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$(selector).clone(true)
$(selector).clone(false)
(selector).clone(true)(selector).clone(false)
$(selector).clone(true)
$(selector).clone(false)

True: True parameter is used to specify that the event handler should also be copied.

False: The false parameter is the default parameter of the clone() method which specifies that the event handler should not be copied.

Example1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").clone().appendTo("body");
});
});
</script>
</head>
<body>
<button>Click me</button>
<p>Hello! </p>
</body>
</html>
<!DOCTYPE html> <html> <!DOCTYPE html> <html> <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").clone().appendTo("body"); }); }); </script> </head> <body> <button>Click me</button> <p>Hello! </p> </body> </html>
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").clone().appendTo("body");
});
});
</script>
</head>
<body>
<button>Click me</button>
<p>Hello! </p>
</body>
</html>

Example2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
("#div2").clone().appendTo("body");
$("#div3").clone().appendTo("body");
});
});
</script>
</head>
<body>
<button>Click me</button>
<div id="div1" style="width:40px;height:80px;background-color:red;border-radius:20px"></div><br>
<div id="div2" style="width:40px;height:80px;background-color:yellow;border-radius:20px;"></div><br>
<div id="div3" style="width:40px;height:80px;background-color:green;border-radius:20px"></div>
</body>
</html>
<!DOCTYPE html> <html> <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ ("#div2").clone().appendTo("body"); $("#div3").clone().appendTo("body"); }); }); </script> </head> <body> <button>Click me</button> <div id="div1" style="width:40px;height:80px;background-color:red;border-radius:20px"></div><br> <div id="div2" style="width:40px;height:80px;background-color:yellow;border-radius:20px;"></div><br> <div id="div3" style="width:40px;height:80px;background-color:green;border-radius:20px"></div> </body> </html>
<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
("#div2").clone().appendTo("body");
$("#div3").clone().appendTo("body");
});
});
</script>
</head>
<body>
<button>Click me</button>
<div id="div1" style="width:40px;height:80px;background-color:red;border-radius:20px"></div><br>
<div id="div2" style="width:40px;height:80px;background-color:yellow;border-radius:20px;"></div><br>
<div id="div3" style="width:40px;height:80px;background-color:green;border-radius:20px"></div>
</body>
</html>