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
querySelector/querySelectorAll accept any CSS selector.
Use textContent (safe) not innerHTML to set user-generated text.
classList.toggle() is perfect for adding/removing active states.
Event delegation = one listener on parent for all child elements.
Always call e.preventDefault() to stop default form/link behavior.