clone() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:clone

Description

The clone() method makes a copy of selected elements, including child nodes, text and attributes.

Syntax

$(selector).clone(booleanvalue);
Parameter Description
true event handlers should be copied
false Default. Specifies that event handlers should not be copied

The following code shows how to Clone all <p> elements and insert them at the end of the <body> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").clone().appendTo("body");
    });/*  ww w  . ja va 2 s  .  c o m*/
});
</script>
</head>
<body>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Clone all p elements, and append them to the body element</button>

</body>
</html>

Related Tutorials