Last Updated: February 25, 2016
·
1.405K
· moox

Empty a DOM element

Short snippet of JavaScript to empty an HTML DOM element without memory leak:

while(el.lastChild) delete el.removeChild(el.lastChild);

3 Responses
Add your response

looks similar to jQuery('something').empty().

search for empty: function in the source to see how they do it

over 1 year ago ·

@zaus jQuery.fn.empty()use a private method now https://github.com/jquery/jquery/blob/5179fa00b839ebc70ec38fb0cf6cb8f8c412f37e/src/manipulation.js#L96
But it use the same while trick not so long ago.

over 1 year ago ·

that's what i thought i was pointing to -- your ref is newer than latest, so it's a little different. i was only mentioning it as a comparison between your tip and how jQuery does it.

over 1 year ago ·