srini
srini

Reputation: 89

Removing a child div from a parent div in jquery

I am trying to delete the child element in the dom from its parent using jquery.

Here is the code snippet.

$('#delete').live('click' , function() {

    var strchild = m.split("/",2)[1];
    var c = group.children(strchild);
    c.remove();

});

strchild contains the id of the child element. group is the parent object. I am getting the right child element in the variable c. But the remove function fails.

Can some help me out here.

Thanks.

Upvotes: 1

Views: 5091

Answers (1)

yoozer8
yoozer8

Reputation: 7489

If you have

strchild

as the id of the element you want to remove, you can do

$("#" + strchild).remove()

assuming it is the only element with that id (it should be, that's the whole point of id).

EDIT:

With multiple ids, you would need to reference the parent specifically. This is very simple, since you say in your question that group is the parent object. This answer assumes it is the object itself, rather than the id, as your code sample implies.

$("#" + strchild, group).remove()

Adding the second argument here constrains the selector to the specifications of that second argument. So this will search the parent (group) for an element with the id strchild, and then remove that element.

Upvotes: 4

Related Questions