Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does <span> get removed automatically if innerHTML changes?

Seems like a really simple question, but I can't find a solid answer for it no matter where I search =( It might be an obvious answer but it just bothers me until I can be sure.

I have this:

someDiv.innerHTML = "Some <span style='color:red'>" + var + "</span> text from variable";

however later on when a user changes some things, it might not require the variable so it transforms to:

someDiv.innerHTML = "A simple sentence";

so my question is, does the previous span get removed automatically? I don't have to worry about tons of <span>'s being somewhere to never be heard of or seen from again? =)

Thanks for clearing out any confusion for me and sorry in advance for the trouble. Thanks again for any help.

like image 973
Hate Names Avatar asked Sep 05 '13 06:09

Hate Names


1 Answers

Assigning string to innerHTML completely erases old content of the DOM element. So the answer is yes! When the spans you mentioned are removed from the DOM tree, their memory footprint left to the browser's garbage collector, and you should not worry about them.

like image 123
Kemal Dağ Avatar answered Nov 12 '22 04:11

Kemal Dağ