Last Updated: February 25, 2016
·
7.704K
· tryhendri

Remove cloning result on Jquery

Based on this http://jsfiddle.net/Tw8z4/1/ They are two option to remove clone result

If I am using Jquery 1.9 and above I assign true parameter on .clone() method

<div class="duplicate">
<p>Form Duplicate</p> 
<a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>

<button>Clone</button>

 $(".remove").click(function(e) {
 $(this).closest(".duplicate").remove();
   e.preventDefault();
 });

$("button").click(function() {
  $(".duplicate:last").clone(true).insertBefore(this);
});

If I am using jquery 1.8.7 below, I don't need to assign true parameter on .clone() method. I just change click to .live(). .live() function was removed in jquery 1.9

$(".remove").live("click", function(e) {
    $(this).closest(".duplicate").remove();
    e.preventDefault();
});

$("button").click(function() {
    $(".duplicate:last").clone().insertBefore(this);
});