According to DOM level 4 specs, which is the current version in development, there are some new handy mutation methods available:
The bulletproof way
Assuming we have the following HTML element:
var elem = document.getElementById("myDiv"); elem.parentNode.removeChild(elem);
The jQuery way
The new way. FTW.
Having already defined the variable
elem, here’s how to properly remove that node:
var elem = document.getElementById("myDiv"); elem.remove();
Notice the jQuery similarity? This new way is simpler and much more intuitive… for the win.
However, if you’re looking for a larger browser support, there are some polyfills that definitely worth an eye on:
P.S. Beside these new methods, don’t forget about the
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)