I have a form
with a button
that is able to dynamically add a div
that contains a paragraph
and a "remove" button
:
<!--form-->
<form></form>
<!--dynamically add div with paragraph and remove button-->
<button onclick="addParagraph()">Add Paragraph</button>
<script>
//remove div
$(".remove").click(function() {
$(this).parent.remove();
});
//add paragraph div
function addParagraph(){
$("form").append('<div><textarea></textarea><button class="remove">Remove</button></div>');
}
</script>
Unfortunately, if I add more than one div and click a remove button, it removes all of the dynamically generated divs. How can I get it so it just removes the one div?
I have also tried $(this).closest("div").remove();
, but same result.