Everything so far has been JavaScript talking to itself. The DOM is how it talks to the page.
DOM stands for Document Object Model. When the browser parses your HTML, it builds a tree of objects: the <html> element, inside it <body>, inside that your sections, headings, forms, and so on. JavaScript can read and change that tree, and the browser updates the screen.
Selecting elements
const title = document.querySelector("#title");
const result = document.querySelector(".result");
const inputs = document.querySelectorAll("input");
querySelectorreturns the first match, ornull.querySelectorAllreturns a NodeList of all matches.- The selector is the same CSS syntax you already know:
#id,.class,input,.card p. document.getElementById("title")is an older, slightly faster alternative for ids.
Reading and changing
title.textContent = "Today's food";
result.classList.add("is-good");
result.classList.remove("is-warning");
result.classList.toggle("is-hidden");
console.log(input.value); // for form fields
console.log(result.dataset.role); // data-role="result"
textContentgets or sets the text inside an element. It’s safe.innerHTMLgets or sets the HTML inside an element. It’s powerful, but dangerous with user input. More below.classListadds, removes, and toggles classes without touching the rest of the class list.valueis the current value of an input, select, or textarea.datasetreadsdata-*attributes:data-role="result"becomesdataset.role.
Creating elements
const item = document.createElement("li");
item.textContent = "Oatmeal — 320 kcal";
document.querySelector("#food-list").append(item);
Create, fill, append. That’s the pattern. If you need to remove an element: item.remove().
innerHTML and safety
// Fine if the string is yours and contains no user input:
result.innerHTML = "<strong>870</strong> kcal";
// Dangerous: a food name could contain HTML or a script.
list.innerHTML = "<li>" + foodName + "</li>";
If foodName comes from a user and contains <img src=x onerror=...>, you’ve just injected code into your page. That’s XSS. Use textContent for user data, or create elements and set textContent. We’ll use that approach in the capstone.
Try it
This demo selects elements, changes text, adds a class, and creates a new list item. Look at the HTML and the JS side by side.
Console
Console is quiet. Run the code to see output.
Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.
Talk to the page
the plan's verify stepOpen your calculator page and link a script.js file with defer. Select the result paragraph and the food list. Change the result text to '870 kcal', add a class that changes its color, and create two list items for the sample foods.
Log each selected element with console.log. If it says null, fix the selector or the script order.
The mistakes everyone makes
- Script runs before the DOM. Use
deferor place the script at the end. - Wrong selector.
#titleneeds the id to exist;.resultneeds the class. - Using
innerHTMLwith user input. XSS. UsetextContent. - Reading
.valuefrom a div. It’s for form fields. - Replacing
classNameand wiping other classes. UseclassList. - Forgetting to append.
document.createElementcreates an element but doesn’t put it on the page until you append it.
The DOM is the browser's live tree of your HTML.
querySelector gets the first match; querySelectorAll gets all.
textContent is safe for text; innerHTML is for trusted HTML only.
classList and dataset are cleaner than string manipulation.
Create with createElement, fill with textContent, add with append.
Next: events and forms. This is where the calculator finally calculates.
Up next
Events and forms: make it react
A form that doesn't submit anywhere, a button that listens, and a result that updates. This is the lesson where the calculator finally calculates.