The ChildNode.remove()
method removes the object from the tree it belongs to.
Syntax
node.remove();
Example
Using remove()
<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>
var el = document.getElementById('div-02');
el.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.
with(node) {
remove();
}
// ReferenceError: remove is not defined
Polyfill
You can polyfill the remove()
method in Internet Explorer 9 and higher with the following code:
// from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('remove')) {
return;
}
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
Specifications
Specification | Status | Comment |
---|---|---|
DOMThe definition of 'ChildNode.remove' in that specification. | Living Standard | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
remove
|
Chrome
Full support 23 |
Edge
Full support 12 |
Firefox
Full support 23 |
IE
No support No |
Opera
Full support 15 |
Safari
Full support 7 |
WebView Android
Full support ≤37 |
Chrome Android
Full support 25 |
Firefox Android
Full support 23 |
Opera Android
Full support 14 |
Safari iOS
Full support 7 |
Samsung Internet Android
Full support 1.5 |
Legend
- Full support
- Full support
- No support
- No support
See also
- The
ChildNode
pure interface. Object types implementing this pure interface:
CharacterData
,Element
, andDocumentType
.
ChildNode.remove() by Mozilla Contributors is licensed under CC-BY-SA 2.5.