child
The node to append to the given parent node (commonly an element).
A Node
that is the appended child (child
), except when child
is a DocumentFragment
, in which case the empty DocumentFragment
is returned.
HierarchyRequestError
DOMException
Thrown when the constraints of the DOM tree are violated, that is if one of the following cases occurs:
child
is not a Document
, DocumentFragment
, or an Element
.child
would lead to a cycle, that is if child
is an ancestor of the node.child
is not a DocumentFragment
, a DocumentType
, an Element
, or a CharacterData
.Text
, and its parent is a Document
.DocumentType
and its parent is not a Document
, as a doctype should always be a direct descendant of a document.Document
and child
is a DocumentFragment
with more than one Element
child, or that has a Text
child.child
would lead to Document
with more than one Element
as child.If the given child is a reference to an existing node in the document, appendChild()
moves it from its current position to the new position â there is no requirement to remove the node from its parent node before appending it to some other node. This means that a node can't be in two points of the document simultaneously. The Node.cloneNode()
method can be used to make a copy of the node before appending it under the new parent. Copies made with cloneNode
are not automatically kept in sync.
appendChild()
returns the newly appended node, instead of the parent node. This means you can append the new node as soon as it's created without losing reference to it:
const paragraph = document.body.appendChild(document.createElement("p"));
// You can append more elements to the paragraph later
On the other hand, you cannot use appendChild()
in a fluent API fashion (like jQuery).
// This doesn't append three paragraphs:
// the three elements will be nested instead of siblings
document.body
.appendChild(document.createElement("p"))
.appendChild(document.createElement("p"))
.appendChild(document.createElement("p"));
Example Append a paragraph to the body
// Create a new paragraph element, and append it to the end of the document body
const p = document.createElement("p");
document.body.appendChild(p);
Creating a nested DOM structure
In this example, we attempt to create a nested DOM structure using as few temporary variables as possible.
const fragment = document.createDocumentFragment();
const li = fragment
.appendChild(document.createElement("section"))
.appendChild(document.createElement("ul"))
.appendChild(document.createElement("li"));
li.textContent = "hello world";
document.body.appendChild(fragment);
It generates the following DOM tree:
<section>
<ul>
<li>hello world</li>
</ul>
</section>
Specifications Browser compatibility
Loadingâ¦
See alsoNode.removeChild()
Node.replaceChild()
Node.insertBefore()
Node.hasChildNodes()
Element.insertAdjacentElement()
Element.append()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.5