Document
Members of the Document interface, reached through the ambient document global: querying the tree, creating nodes, and reaching the document's structural elements. The factories are mutation-guarded and throw NoModificationAllowedError on a read-only document.
Querying
querySelector
querySelector(selector) → the first matching element in document order, or null. An invalid selector throws SyntaxError.
Called on document, it searches the whole document. Called on an element or a document fragment, it follows the DOM standard, as in a browser:
- it returns only the receiver's descendants, never the receiver itself
- the selector is still matched against the whole tree, so
section.querySelector("div p")finds apinsidesectioneven when itsdivancestor is outsidesection - on an element,
:scopemeans the receiver, sosection.querySelectorAll(":scope > p")returns onlysection's child paragraphs - a document fragment is not an element, so on a fragment
:scopematches nothing, as in a browser:frag.querySelectorAll(":scope > p")returns nothing. The fragment is never a parent for a combinator, so* > pdoes not return its top-level paragraphs either - an element that is not attached to the document (from
createElement, for example) searches its own subtree - a
<template>'s content is never searched: on a template the query finds nothing, and a template inside the receiver is found but not searched into
const title = document.querySelector("main h1");
const firstItem = document.querySelector("ul#todo").querySelector(":scope > li");
querySelectorAll
querySelectorAll(selector) → a static NodeList of every match, in document order. Invalid selector throws SyntaxError. It is scoped exactly as querySelector is: the whole document from document, and only the receiver's descendants from an element or fragment.
for (const li of document.querySelectorAll("ul#todo > li")) {
li.classList.add("seen");
}
getElementById
getElementById(id) → the element with that id, or null. The match is a literal string comparison on the id attribute — no CSS parsing, so ., :, and [ match literally. It is a member of Document only, as in the browser, and searches the whole document.
const cart = document.getElementById("cart");
getElementsByTagNameNS
getElementsByTagNameNS(namespace, localName) → a NodeList, scoped to the receiver's descendants (excluding the receiver). "*" matches any namespace or any local name.
const cells = row.getElementsByTagNameNS("*", "td");
Creating nodes
Factory methods returning detached nodes. All are mutation-guarded.
createElement
createElement(tag) → a detached element. tagName later reports the tag uppercased (no namespace).
const li = document.createElement("li");
li.textContent = "New item";
createElementNS
createElementNS(namespace, qualifiedName) → a detached namespaced element. qualifiedName is stored verbatim (case preserved); namespace is recorded as the element's namespace URI. Throws NamespaceError for a malformed qualified name (empty, more than one :, a leading/trailing :, or a prefix with an empty namespace). No xmlns: declaration is auto-emitted.
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
createTextNode
createTextNode(data) → a detached text node.
const t = document.createTextNode("Hello");
createComment
createComment(data) → a detached comment node.
const c = document.createComment("build: 2026-07-02");
createDocumentFragment
createDocumentFragment() → a detached fragment. Appending the fragment moves its children into the target (leaving the fragment empty).
const frag = document.createDocumentFragment();
for (const name of names) {
const li = document.createElement("li");
li.textContent = name;
frag.appendChild(li);
}
list.appendChild(frag); // moves every <li> in at once
Document structure
Accessors that reach the document's structural elements — each returns the element or null.
documentElement
The root element (<html>).
const lang = document.documentElement.getAttribute("lang");
head / body
The <head> and <body> elements.
document.body.classList.add("ready");
See also
- Element — what you do with a queried element
- Node — placing created nodes into the tree
- DOMParser and XMLSerializer — build a fresh writable document