Beginner Topic 13 of 51 Web Development
DOM
Tree representation of a page that scripts can modify.
How it works
The Document Object Model is the browser's in-memory tree representation of an HTML document, where each element, attribute and piece of text is a node that JavaScript can read and modify. APIs such as querySelector, createElement, appendChild, classList and textContent let scripts change structure, content and style, and the browser re-renders accordingly. Live collections and event bubbling are part of its behavior.
Step-by-step walkthrough
- The browser builds a tree of objects from the HTML.
- The body is one child.
- The div is a node in the tree.
- Scripts select nodes with querySelector.
- Changing a node updates what the user sees.
Where it is used
Updating the page without reloading, building interfaces dynamically, reading form values and implementing widgets use the DOM. Frameworks like React abstract it with a virtual DOM.
Common pitfall
DOM operations are comparatively slow, and repeated layout reads and writes cause layout thrashing; batch changes. Setting innerHTML with untrusted input creates cross-site scripting vulnerabilities, so use textContent or sanitize. Holding references to removed nodes leaks memory. Selecting by fragile selectors breaks when markup changes.
Where it fits in the course
DOM is topic 13 of 51 in Web Development, rated Beginner. It follows JavaScript Basics. The next topic is Events.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is DOM?
Where do people use DOM?
What is the most common mistake with DOM?
What should I learn before this topic?
How can I practice it?
Is DOM hard to learn?
How long does it take to learn DOM?
Which topics are related to DOM?
What should I study after DOM?
Related topics
- JavaScript Basics (Web Development)
- Events (Web Development)
- Media Queries (Web Development)
- Fetch API (Web Development)