QuickDevGuide

Home › JavaScript

DOM Manipulation — JavaScript

The Document Object Model (DOM) is the browser's live, in-memory representation of an HTML page as a tree of objects. Every tag becomes a node in the tree. JavaScript can read, modify, add, or remove any node — making the page dynamic and interactive. Selecting Elements getElementById("id") is fastest for single elements. querySelector(cssSelector) selects the first match for any CSS selector. querySelectorAll(cssSelector) returns a static NodeList of all matches. Reading & Modifying element.textContent reads/sets text (safe from XSS). element.innerHTML reads/sets raw HTML — never set with user input. element.getAttribute("href") reads an attribute. element.setAttribute("src", "new.jpg") sets one. element.style.color = "red" sets inline styles. classList.add(), .remove(), .toggle()…

Key Takeaways