Node
Members shared by every node — the document, elements, text, comments, and fragments alike: traversal, tree mutation, character data, and metadata. Element-only navigation (children, firstElementChild, …) is on the Element page. All mutators are guarded and throw NoModificationAllowedError on a read-only document.
Traversal
All-kinds navigation (text and comment nodes are included). Each returns a handle or null.
| Accessor | Value |
|---|---|
parentNode |
The parent, or null. |
parentElement |
The parent only if it is an element, else null. |
firstChild / lastChild |
First / last child of any kind, or null. |
previousSibling / nextSibling |
Adjacent sibling of any kind, or null. |
childNodes |
A static NodeList of all child kinds. |
ownerDocument |
The owning document — null when called on the document itself. |
hasChildNodes
hasChildNodes() → boolean.
if (node.hasChildNodes()) {
for (const child of node.childNodes) { /* text and elements */ }
}
Metadata
| Accessor | Value |
|---|---|
nodeType |
The numeric type code — compare against the Node constants. |
nodeName |
Element = uppercased tag; otherwise #text, #comment, #document, #document-fragment, the doctype name, or a PI target. |
if (node.nodeType === Node.ELEMENT_NODE) { /* it's an element */ }
Character data
textContent
Get concatenates all descendant text (a comment yields its own data). Set replaces every child with a single text node — an empty string leaves no children. Available on every node; guarded.
cell.textContent = "42";
const words = article.textContent.split(/\s+/).length;
data / length
data (get/set) and length (get) are CharacterData members, so they apply to text and comment nodes: data is the character payload, length its Unicode character count. On a node of any other interface they are out of scope, so element.data and element.length are undefined. Setting data is guarded.
const c = document.createComment("draft");
c.data; // "draft"
c.length; // 5
c.data = "final";
Tree mutation
A node from another document is adopted (deep-copied); a DocumentFragment argument moves its children (leaving the fragment empty).
appendChild
appendChild(child) → the appended child. Appending a node to its own descendant or itself throws HierarchyRequestError.
list.appendChild(document.createElement("li"));
insertBefore
insertBefore(node, reference) → the inserted node. A null/omitted reference behaves like appendChild; otherwise reference must be a current child, else NotFoundError.
list.insertBefore(newItem, list.firstChild); // insert at the top
removeChild
removeChild(child) → the removed child (which stays live with parentNode === null). child must be a child, else NotFoundError.
list.removeChild(list.lastChild);
replaceChild
replaceChild(newNode, oldNode) → oldNode. oldNode must be a child, else NotFoundError.
list.replaceChild(document.createElement("li"), staleItem);
cloneNode
cloneNode(deep?) → a detached clone (deep defaults to false). Rejected on a read-only document — the clone would allocate into the read-only arena.
const copy = template.cloneNode(true);
contains
contains(other) → boolean; false across documents.
if (main.contains(node)) { /* node is inside <main> */ }
Node constants
The Node global exposes the standard node-type constants, for comparison against nodeType:
Node.ELEMENT_NODE (1) · Node.TEXT_NODE (3) · Node.COMMENT_NODE (8) · Node.DOCUMENT_NODE (9) · Node.DOCUMENT_TYPE_NODE (10) · Node.DOCUMENT_FRAGMENT_NODE (11)
See also
- Element — attributes, content, and element-only traversal
- Document — creating the nodes you insert
- NodeList — iterating
childNodes - DOMException — the error type these mutators throw