I have a jQuery-based form where you can add extra people to the application. I'm cloning the first fieldset and adding it onto the end up to a max of 3 additional people. When you've added 1 extra person then you have the option to remove that person.
However, my remove button isn't working. It was, earlier, until I added the extra functions to the cloning to change the ids of other elements within the fieldset.
I'm using:
$(".remove").click(function() {
$(this).parent().remove();
which was working originally but now it's not and I can't figure out why.
I've taken out the lines that stop the first 'delete this person' just to show that the first one still works but the rest don't. (I'll be positioning the first one off stage eventually when it's fixed)
Probably easier to see it so I put it up here.
So essentially, any ideas why my 'delete this person' isn't working on everything but the first section?
It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.
The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.
Try:
$(document).on('click', '.remove', function() {
$(this).parent().remove();
});
Events are bound on page load so newly added element aren't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With