Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.parent().remove() issue

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?

like image 579
leapin_leprechaun Avatar asked May 15 '11 18:05

leapin_leprechaun


People also ask

What is the use of parent () and child () method in jQuery?

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.

What is parent method in Javascript?

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.


1 Answers

Try:

$(document).on('click', '.remove', function() {
    $(this).parent().remove();
});

Events are bound on page load so newly added element aren't.

like image 192
PeeHaa Avatar answered Sep 30 '22 19:09

PeeHaa