I have a divs inside parent div:
<div id="parent">
<div id="child">some content1</div>
<div id="child">some content1</div>
</div>
I get the parent div using jQuery:
var allDivs= $("#parent").html();
I want to remove child divs from this parent, how I can do this? Child divs should be deleted from jQuery object but not from DOM, is it possible?
2
Answers
Deleting them from the jQuery object which references the DOM would remove them from the DOM. However, you can clone that object and then modify the clone all you like.
For example:
As you can see in the second operation above, after selecting the "parent" you can call
.clone()
to create an in-memory clone of the structure, then.empty()
to remove all child elements from that (in-memory) parent element.Edit: If you want to remove only some child elements then instead of
.empty()
you’d want to specifically target and remove those elements. For example:.empty method should do the trick for you…