jQuery clone()

Introduction

Clone all <p> elements and insert them at the end of the <body> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from w ww.j a v a2  s .co m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").clone().appendTo("body");
  });
});
</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>

The clone() method makes a copy of selected elements including child nodes.

$(selector).clone(true|false)
Parameter Description
true event handlers also should be copied
false Default. event handlers should not be copied



PreviousNext

Related