On this page
Nodes & Elements
Prev: dom | Up: dom | Next: Events & Delegation
Rel: HTML Elements, CSS Selectors, Reflow, Repaint & Performance
Ref: MDN Web Docs — Element
Tags: #public #web #platform #dom
Contents
Querying & Traversing
const card = document.querySelector(".card[data-id='42']");
const items = document.querySelectorAll(".card li"); // Static NodeList
const list = card?.closest("section"); // Nearest matching ancestor
Safe Mutation & Batching (DocumentFragment)
Prefer textContent over innerHTML for untrusted strings, and batch multiple insertions in a DocumentFragment so the browser performs a single DOM update:
const frag = document.createDocumentFragment();
for (const label of ["Alpha", "Beta", "Gamma"]) {
const li = document.createElement("li");
li.textContent = label;
li.classList.add("item");
frag.append(li);
}
document.querySelector("#list")?.replaceChildren(frag);
Linked from
- tuts/css/selectors
- tuts/html
- tuts/html/attributes/global_attributes
- tuts/html/elements
- tuts/html/elements/forms
- tuts/html/elements/interactive
- tuts/html/elements/landmarks
- tuts/html/elements/media
- tuts/html/elements/text
- tuts/web/dom
- tuts/web/dom/events_and_delegation
- tuts/web/dom/nodes_and_elements
- tuts/web/security/csp_cookies_and_mitigations
Related notes
- tuts/web/dom Linked here; links to this page; shared tags: #dom, #platform, #web
- tuts/web/dom/events_and_delegation Linked here; links to this page; shared tags: #dom, #platform, #web
- tuts/css/selectors Linked here; links to this page; shared tags: #web
- tuts/html/elements Linked here; links to this page; shared tags: #web
- tuts/web/rendering/reflow_repaint_and_performance Linked here; shared tags: #platform, #web
- tuts/web/security/csp_cookies_and_mitigations links to this page; shared tags: #platform, #web
- tuts/html links to this page; shared tags: #web
- tuts/html/attributes/global_attributes links to this page; shared tags: #web
- tuts/html/elements/forms links to this page; shared tags: #web
- tuts/html/elements/interactive links to this page; shared tags: #web