I have a HTML code to add several elements using button click and remove each element separately. The problem is remove function is not working. I need to remove the exact selected element. How to do this?
//add item
$("button").on("click", function() {
$(".main").append($(".sub:last").clone().html());
$("span:last").after("<button class='removeDiv'>Remove</button>");
});
//remove item
$(".removeDiv").on("click", function() {
$(this).closest(".inner").remove();
});
.inner {
margin-bottom : 20px;
}
.removeDiv {
margin-left : 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main">
<div class="sub">
<div class="inner">
<span>New Content</span>
</div>
</div>
</div>
<button>Add New</button>