JomMula

Frontend Development: HTML to React · JavaScript · Lesson 16

The DOM: select, read, change

The DOM is the browser's live model of your HTML. Learn to select elements, read their values, change their text and classes, and create new elements.

8 min readJavaScriptDOMUpdated October 2026

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");
  • querySelector returns the first match, or null.
  • querySelectorAll returns 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"
  • textContent gets or sets the text inside an element. It’s safe.
  • innerHTML gets or sets the HTML inside an element. It’s powerful, but dangerous with user input. More below.
  • classList adds, removes, and toggles classes without touching the rest of the class list.
  • value is the current value of an input, select, or textarea.
  • dataset reads data-* attributes: data-role="result" becomes dataset.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.

Preview

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 step

Open 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 defer or place the script at the end.
  • Wrong selector. #title needs the id to exist; .result needs the class.
  • Using innerHTML with user input. XSS. Use textContent.
  • Reading .value from a div. It’s for form fields.
  • Replacing className and wiping other classes. Use classList.
  • Forgetting to append. document.createElement creates 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.

Previous: Loops and array methods
17

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.

Read now