php_nub_qq
php_nub_qq

Reputation: 16017

Parent innerHTML and child nodes

I want to ask if I want to remove an HTML element's children but I don't necessarily want to loop through them, would setting the parent's ìnnerHTML to null or an empty string remove the children from memory, not just from the visual part of the document?

Upvotes: 0

Views: 1435

Answers (2)

nicael
nicael

Reputation: 18997

Yes, it will completely remove children.
For example, you have:

<div id="a_div">
<input type='button'><br>
<img src='image.png'>
</div>

Then

document.getElementById("a_div").innerHTML="<input type='button'><br><img src='image.png'>";

So if you set innerHTML to "" (document.getElementById("a_div").innerHTML="";), then a_div will be

<div id="a_div">
</div>

Upvotes: 1

Mayank Tripathi
Mayank Tripathi

Reputation: 1342

If you are using jQuery then empty() function used on the parent element will remove the elements from the DOM.

Upvotes: 0

Related Questions