Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Node
  4. removeChild()

Node: removeChild() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheremoveChild() method of theNode interfaceremoves a child node from the DOM and returns the removed node.

Note:As long as a reference is kept on the removed child,it still exists in memory, but is no longer part of the DOM.It can still be reused later in the code.

If the return value ofremoveChild() is not stored, and no other reference is kept,it will beautomatically deleted from memory after a short time.

UnlikeNode.cloneNode() the return value preserves theEventListener objects associated with it.

Syntax

js
removeChild(child)

Parameters

child

ANode that is the child node to be removed from the DOM.

Return value

The removedchild node.

Exceptions

NotFoundErrorDOMException

Thrown if thechild is not a child of the node.

TypeError

Thrown if thechild isnull.

Examples

Simple examples

Given this HTML:

html
<div>  <div></div></div>

To remove a specified element when knowing its parent node:

js
const parent = document.getElementById("parent");const child = document.getElementById("child");const throwawayNode = parent.removeChild(child);

To remove a specified element without having to specify its parent node:

js
const node = document.getElementById("child");if (node.parentNode) {  node.parentNode.removeChild(node);}

To remove all children from an element:

js
const element = document.getElementById("idOfParent");while (element.firstChild) {  element.removeChild(element.firstChild);}

Causing a TypeError

html
<!--Sample HTML code--><div></div>
js
const parent = document.getElementById("parent");const child = document.getElementById("child");// Throws Uncaught TypeErrorconst garbage = parent.removeChild(child);

Causing a NotFoundError

html
<!--Sample HTML code--><div>  <div></div></div>
js
const parent = document.getElementById("parent");const child = document.getElementById("child");// This first call correctly removes the nodeconst garbage = parent.removeChild(child);// Second call throws NotFoundErrorparent.removeChild(child);

Specifications

Specification
DOM
# dom-node-removechild

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp