Last Updated: February 25, 2016
· 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
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 ·