user1637281
user1637281

Reputation:

Remove an element ( and its children ) from the DOM

Via the MDN reference I want to simply do:

parent_element.removeChild(child_element);

However, in this particular case child_element has its own child elements.

Can I assume that this will not cause any problems and that they too will be removed.

The examples given in the reference did not make complete sense.

Upvotes: 0

Views: 677

Answers (4)

mP.
mP.

Reputation: 18266

If the children didnt get removed, where or how could they exist in the DOM, unless someone put them somewhere else.

Upvotes: -2

Brandon
Brandon

Reputation: 39222

Yes when you remove an element from the DOM, all of its children are removed with it. If you are working with a modern browser, this is pretty safe. Older browsers tended to get memory leaks if you did not first remove all your event handlers before removing the elements.

Upvotes: 5

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

yes you can assume that the child's children will be removed

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382464

Yes, all elements that are childs of a removed element are removed. You don't need to implement a deep removal yourself.

Upvotes: 2

Related Questions