Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I remove this form?

Tags:

jquery

dom

forms

I'm using the .remove() function from within jQuery to try and remove a form from a DIV on a page that I've created but it doesn't see to work. Is there anything wrong with my code?

I've used an alert to see if the actual line is being fired and it definitely is. Basically I have a delete button on a page, which I'm hoping will remove the last form added but it doesn't do this. I saw the .last() function but don't know if I can use it in this context?

HTML:

<input type="button" value="Delete Row" class="removePlacement button blue" />

<div id="placement">
<form id="placement-form" class="placement-form">
    <!-- items -->
</form>
 <form id="placement-form1" class="placement-form">
    <!-- items -->
</form>
 <form id="placement-form2" class="placement-form">
    <!-- items -->
</form>
 <form id="placement-form3" class="placement-form">
    <!-- items -->
</form>
</div>

jQuery:

$(function() {
$('.removePlacement').click(function() {
    $('#placement').remove('form').last();
    // alert('0');
    }); 
});
like image 843
zik Avatar asked Feb 28 '26 18:02

zik


1 Answers

Try:

$(function() {
    $('.removePlacement').click(function() {
        $('#placement').find("form:last").remove();
        // alert('0');
    }); 
});
like image 126
karim79 Avatar answered Mar 05 '26 19:03

karim79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!