Skip to content

JavaScript DOM Basics: querySelector, textContent, classList and createElement

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Until now your code has talked only to the console. This lesson connects it to the page itself. When a browser loads HTML, it builds a tree of objects in memory, one for every element, and JavaScript can read and change that tree. Change it, and the page on screen changes straight away. That is how a calculator shows its result, how a cart adds a new line and how a form marks a field in red. Everything you learnt about variables, functions and objects now has somewhere visible to go.

Quick answer

The DOM (Document Object Model) is the browser’s live tree of the page. Put your <script> at the end of <body>, or add defer in the head, so the elements exist before the code runs. Select one element with document.querySelector("#total"), using any CSS selector, and all matches with querySelectorAll(".price"). Change text with el.textContent = "105 AED", classes with el.classList.add("paid"), and attributes with el.setAttribute("href", url). Build new elements with document.createElement("li"), then add them with list.append(li) and remove them with el.remove(). Avoid putting user input into innerHTML.

30 min read and practice · Content checked on 6 November 2026

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.

Try it: A page that JavaScript fills in
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.