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);
Written by Maxime Thirouin
Related protips
3 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/54526/d940dd93751cea6229c4b9b2098b7f39.png)
looks similar to jQuery('something').empty()
.
search for empty: function
in the source to see how they do it
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/38172/d2a1c95e2af59538301c3ee6d2907f6f.jpeg)
@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
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/54526/d940dd93751cea6229c4b9b2098b7f39.png)
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Dom
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#