JavaScript and the DOM
Level 5 · JavaScript in the page
How JavaScript finds a part of the page and changes it. What the DOM tree is, and how an event listener reacts to a click.
Course contentsLesson 12 of 14
Level 1 · How a website works
Level 3 · CSS basics
Level 4 · Page layout
Level 5 · JavaScript in the page
After this lesson you can
- Find an element with querySelector
- Change its text or class from JavaScript
- React to a click with an event listener
13 mincovers 3 glossary entries
Worth reading first: HTML Elements and Attributes
How JavaScript changes a page after it loads
JavaScript is the language every web browser runs. It first appeared in 1995, inside a web browser of the time. It changes a page through the DOM, the Document Object Model. The DOM is a live tree the browser builds from the HTML, with one node for every element. Change a node, and the page on screen changes too.
The tree follows the HTML. The document sits at the top, then <html>, then <head> and <body>, and every element inside them is a branch. Text is a node as well. The HTML file is only the starting point. Once the page loads, JavaScript can add, move or remove nodes at will.
To change something, the code must first find it. The querySelector function takes the same selectors as CSS. So querySelector("#total") finds the element with that id. Then the code can set textContent to change the words. Or it can add a class and let the CSS do the rest.
An event is a sign that something happened: a click, a key press, a form being sent, or the page done loading. The addEventListener function ties code to one event on one element. When the event fires, the browser runs that code. That is how a button does something when pressed.
Some events have a built-in action. Sending a form makes the browser load a new page, and clicking a link follows it. Calling preventDefault() inside the listener stops that. Then your own code can take over, such as sending the form in the background.
Where the script sits matters. A script at the top of the page runs before the body exists, so querySelector finds nothing. Put the <script> tag at the end of the body, or add the defer attribute so it waits until the HTML has been read.
People often confuse the HTML file with the page they see. The page source shows the file as it arrived. The Elements panel in DevTools shows the DOM as it is now, after the scripts have run. When the two differ, the DOM is the one on screen.
const button = document.querySelector("#add");
const count = document.querySelector("#count");
let n = 0;
button.addEventListener("click", () => {
n = n + 1;
count.textContent = n;
});Worked example
Lucy adds a basket counter
Lucy's bakery page has 8 Add buttons and a basket count in the corner. Before, each click sent the whole page again and took 2 seconds.
She adds one click listener per button that raises the count in the DOM. The number now changes at once, with no page load at all.
The words for this lesson3
Check yourself
Why does my script find nothing when it runs at the top of the page?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.