I have cloned divs containing buttons that clone their parents. Upon trying to click the buttons, events are triggered 2n times such that clicking the second clone produces 4 other clones and so on:
$(".add").off("click").on("click", function(e) {
e.stopPropagation();
e.stopImmediatePropagation();
$(".cloneable").clone(true).appendTo(".container");
});
$(".rem").off("click").on("click", function() {
if (document.getElementsByClassName('container')[0].childElementCount > 1) {
$(".cloneable:last").remove();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="cloneable">
<div>
<a class="btn btn-primary add" role="button">Add cell</a>
<a class="btn btn-primary rem" role="button">Remove cell</a>
</div>
<iframe src="index.php"></iframe>
</div>
</div>