In this lesson you will
- Explain what the DOM is and place scripts so the elements exist
- Select elements with querySelector, querySelectorAll and getElementById
- Change text, attributes and classes, and know why textContent is safer than innerHTML
- Create, add and remove elements to build a list from data
What the DOM is and where the script goes
The browser reads your HTML from top to bottom and turns each tag into an object called a node, arranged in a tree: html contains head and body, body contains your headings and paragraphs, and so on. This tree is the DOM. JavaScript reaches it through the global document object. The DOM is not your HTML file: it is the browser’s live copy, so changes affect what people see right now, and reloading the page throws them away.
A script runs as soon as the browser reaches it. If it sits in the <head> and asks for an element further down, that element does not exist yet and you get null. There are two clean fixes: put the <script> just before </body>, as the examples here do, or keep it in the head as an external file with the defer attribute, <script src="app.js" defer></script>, which waits until the whole page has been read. Both are standard; defer is the usual choice for real sites.
Console
- Console messages, errors, form data and clicked links appear here.
The script comes after the elements, so it can find them. Asking for an element that does not exist gives null, which is the first thing to check when a DOM line fails.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the DOM in simple words?
It is the browser’s model of the page as a tree of objects, one for each element, that JavaScript can read and change. Your HTML file is the recipe; the DOM is the dish on the table. Changing the DOM changes what people see without touching the file on the server.
Should I use querySelector or getElementById?
Both are fine. querySelector is more flexible because it takes any CSS selector, so many teams use it everywhere for consistency. getElementById only finds ids and is slightly faster, which almost never matters in practice.
What is the difference between textContent, innerText and innerHTML?
textContent is all the text, fast and safe. innerText is the text as it appears on screen, so it skips hidden elements and is slower to read. innerHTML is the HTML inside the element and turns strings into real elements, which is why it must never receive untrusted text.
Does changing the DOM help or hurt SEO?
Google can run JavaScript and see changes made by it, but it may do so later than it reads the HTML. Put the main content of a page, such as headings and explanations, in the HTML itself, and use JavaScript for results, interaction and extras. That is how the tools on this site are built.
Do I still need jQuery to work with the DOM?
No. Methods such as querySelector, classList, append and remove work in every current browser and cover what jQuery was mostly used for. You will still meet jQuery in older sites, and it reads easily once you know the plain DOM methods it wraps.
Sources
Facts in this lesson were checked against these official pages on 6 November 2026.
- MDN: DOM scripting introduction
- MDN: Document.querySelector()
- MDN: Document.querySelectorAll()
- MDN: Node.textContent
- MDN: Element.innerHTML (security considerations)
- MDN: Element.classList
- MDN: Document.createElement()
- MDN: Element.append()
- MDN: The script element
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.