jQuery clone() Copy event handlers

Introduction

Copy the first p element including event handlers, and append to 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  w w.j ava  2 s .  c o  m
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("body").append($("p:first").clone(true));
  });
  $("p").click(function(){
    $(this).animate({fontSize: "+=1px"});
  });
});
</script>
</head>
<body>

<p>Click this paragraph to increase text size.</p>
<p>This is another paragraph.</p>

<button>Copy</button>

</body>
</html>



PreviousNext

Related