Does <span> automatically remove if innerHTML changes?

This seems to be a very simple question, but I can’t find a solid answer for it, no matter where I looked for = (This may be the obvious answer, but it just bothers me until I can be sure.

I have it:

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

however, in the future, when the user changes some things, he may not require a variable, therefore it is converted to:

someDiv.innerHTML = "A simple sentence";

so my question is: is the previous range deleted automatically? Do I need to worry about having tons of <span>'s somewhere so that they are never heard or seen again? =)

Thank you for freeing me from confusion and regretted troubles in advance. Thanks again for any help.

+7
javascript html
source share
1 answer

Assigning a string to innerHTML completely removes the old contents of the DOM element. So yes! When the gaps you specify are removed from the DOM tree, their memory area remains in the browser garbage collector, and you should not worry about them.

+7
source share

All Articles