JavaScript Simplified · One concept, one day, one project · Day 13 of 32
Introduction to the DOM
How JavaScript reaches into a web page and changes it. You select an element and change what it says, then build a greeting that writes a name onto the page.
By the end of today you can
- Explain what the DOM is in plain words
- Select an element with
getElementByIdandquerySelector - Change what an element says from JavaScript
- Build an on-page greeting
Everything so far has lived in the console. Today JavaScript finally touches the page you can see. This is the layer that makes the language feel real: you write code, and the page changes in front of you.
1. What the DOM Is
Definition
The DOM (Document Object Model) is the browser’s live version of your HTML, as objects JavaScript can read and change. When the page loads, the browser turns your HTML into a tree of elements, and JavaScript can reach any branch of that tree.
Real-Life Scenario
Think of your HTML as the blueprint of a house, and the DOM as the actual house once it is built. You cannot move a wall by editing the blueprint after the fact; you move it in the real house. JavaScript changes the real house, the DOM, and you see it happen.
2. Selecting an Element
Before you can change something, you have to grab hold of it. Two ways cover almost everything.
getElementById("name")finds the one element whoseidis "name".querySelector("...")finds the first element matching a CSS selector, like".title"for a class or"h1"for a tag.
Once you have an element, textContent is the text inside it. Setting it changes what the page shows.
The HTML said "Loading...", but the JavaScript selected the heading and replaced its text. What you see on the page is the DOM after your code changed it.
querySelector
getElementById takes just the id name (no #). querySelector takes a full CSS selector, so querySelector("#title") with a # does the same as getElementById("title"). Use whichever reads clearly; querySelector is the more flexible of the two.
Today’s Mini Project
👋 Interactive Greeting
A page with a name box and a greeting. JavaScript reads the name and writes a personal greeting onto the page. (On Day 17 you will make it update on a click; today it writes on load.)
.value reads what is in an input box. The greeting heading is then set to include that name. Change the box’s value and the greeting would change to match.
Add a second input for a city and change the greeting to "Hello, Ada from Lagos!" by reading both boxes. Expected: the greeting includes both the name and the city.
Add a paragraph with id="sub" and use JavaScript to set its text to today’s goal, like "Today I learn the DOM".
Expected: the paragraph shows your sentence, set from JavaScript.
For the first time, your code changed something you can see. That gap between "code in a file" and "a page that reacts" is the whole reason JavaScript exists, and you just crossed it.
Checkpoint
What is the DOM?
The browser’s live version of your HTML, as objects JavaScript can read and change. Changing the DOM changes what you see on the page.
What is the difference between getElementById and querySelector?
getElementById takes just an id name. querySelector takes a full CSS selector (like .class or #id or a tag) and returns the first match. querySelector is more flexible.
How do you change the text inside an element?
Select the element, then set its textContent, for example title.textContent = "Hello". The page updates to show the new text.
Tick Before You Move On
- ☐ I can explain what the DOM is
- ☐ I can select an element by id and by CSS selector
- ☐ I can change an element’s text from JavaScript
- ☐ I built the Interactive Greeting
Quick Recap
The DOM is the live, changeable version of your HTML. getElementById and querySelector select elements. textContent reads and sets the text inside an element. Changing the DOM changes what the user sees.
Next: Day 14. You changed one heading. Tomorrow you change several parts of a card at once, and meet the difference between textContent and innerHTML.