ChildNode.remove()
The ChildNode.remove()
method removes the object from the tree it belongs to.
Syntax
JavaScript
Copy Code
elementNodeReference.remove();
Example
Using remove()
HTML
Copy Code
<div id="div-01">Here is div-01</div> <div id="div-02">Here is div-02</div> <div id="div-03">Here is div-03</div>
JavaScript
Copy Code
var el = document.getElementById('div-01'); el.nextElementSibling.remove(); // Removes the div with the 'div-02' id
ChildNode.remove()
is unscopable
The remove()
method is not scoped into the with
statement. See Symbol.unscopables
for more information.
JavaScript
Copy Code
with(node) { remove(); } // ReferenceError: remove is not defined
Polyfill
JavaScript
Copy Code
if (!('remove' in Element.prototype)) { Element.prototype.remove = function() { if (this.parentNode) { this.parentNode.removeChild(this); } }; }
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'ChildNode.remove' in that specification. |
Living Standard | Initial definition. |
DOM4 The definition of 'ChildNode.remove' in that specification. |
Recommendation |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support (on Element ) |
23.0 | 23.0 (23.0) | Edge | 10.0 | 7.0 |
Support on CharacterData and DocumentType |
29.0 | 23.0 (23.0) | Edge | 16.0 | 7.0 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support (on Element ) |
(Yes) | 23.0 (23.0) | No support | 10.0 | No support |
Support on CharacterData and DocumentType |
(Yes) | 23.0 (23.0) | No support | 16.0 | No support |
See also
- The
ChildNode
pure interface.
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/childnode/remove