Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is the DOM and How Do You Change a Page with JavaScript?

Web DevelopmentCSSJavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
6 min read

The DOM (Document Object Model) is the browser's live, tree-shaped representation of your HTML page, and JavaScript can read and change it to update what the user sees. When the browser loads a page, every element, attribute and piece of text becomes an object (a node) that your script can find, edit, add or remove. Changing those nodes changes the page instantly without a reload.

This guide explains what the DOM is, how to select elements, and how to change text, attributes, classes and structure safely.

Table of Contents

  • What is the DOM
  • Selecting elements
  • Changing text, attributes and classes
  • Creating, inserting and removing elements
  • textContent versus innerHTML
  • A small working example
  • Frequently Asked Questions
    • Is the DOM part of JavaScript?
    • What is the difference between a node and an element?
    • Why is querySelector returning null?
    • Is getElementById faster than querySelector?
  • Summary
  • Related Articles

What is the DOM

The browser parses your HTML and builds a tree. The document object is the root, and every tag becomes an element node below it.

html

For this page, document contains html, which contains body, which contains the h1 and the ul, and the ul has two li children. Text inside tags is also a node. The tree has relationships such as parent, child and sibling, and you can walk it with properties like parentElement, children and nextElementSibling.

The DOM is not your HTML file. It is a live object built from it, and scripts change the DOM, not the file on the server. If you view the page source after a change, the source stays the same, but the inspector in your browser developer tools shows the updated DOM.

Selecting elements

Before you can change something you need a reference to it. The two modern methods take a CSS selector.

javascript
javascript
Loading syntax highlighting...
text

querySelector returns the first match or null. querySelectorAll returns a static NodeList of all matches, which you can loop with for...of or forEach. Older methods such as getElementById and getElementsByClassName still work, and getElementById is a fast, clear choice when you have an id.

Always make sure the script runs after the elements exist. Put <script type="module" src="app.js"> in the page (module scripts are deferred automatically), or add the defer attribute to a classic script. Otherwise querySelector returns null, and the next line throws a TypeError.

Changing text, attributes and classes

Once you have an element, update it through its properties.

javascript
javascript
Loading syntax highlighting...
TaskCodeNotes
Set plain textel.textContent = "Hi"Safe, never parses HTML
Read or set an attributeel.getAttribute("href"), el.setAttribute("href", url)Works for any attribute
Add, remove, toggle a classel.classList.add("on"), .remove, .togglePreferred over editing className
Inline styleel.style.color = "red"Prefer classes for real styling
Form valueinput.valueCurrent text of an input
Hide an elementel.hidden = trueUses the built-in hidden attribute

Prefer toggling CSS classes over writing many inline styles. Your stylesheet stays in charge of the design, and the script only decides which state applies.

Creating, inserting and removing elements

You can build new elements in JavaScript and add them to the page.

javascript
javascript
Loading syntax highlighting...

createElement makes the element in memory, and nothing is visible until you insert it. append adds at the end, prepend at the start, and before and after insert next to an element. remove() deletes an element from the page. To add many items, build them in a loop and insert them once, or use a DocumentFragment, to avoid repeated layout work.

textContent versus innerHTML

innerHTML reads or sets the HTML inside an element as a string. It is convenient but dangerous when the string includes user input.

javascript
javascript
Loading syntax highlighting...

Inserting untrusted data with innerHTML is a classic cross-site scripting (XSS) hole. Use textContent for text, build elements with createElement, or sanitise the HTML with a trusted library or the browser's Sanitizer API where it is supported. Reserve innerHTML for markup you wrote yourself.

A small working example

This page counts clicks and writes the result into the DOM.

html

The script selects the button, listens for a click, then updates the text and a class each time.

Frequently Asked Questions

Is the DOM part of JavaScript?

No. The DOM is a web platform API defined for browsers, and JavaScript is the language that uses it. Node.js has no DOM by default.

What is the difference between a node and an element?

A node is any item in the tree, including text and comments. An element is a node that comes from an HTML tag. Properties like children list only elements, while childNodes includes all nodes.

Why is querySelector returning null?

The selector does not match, or the script ran before the element was parsed. Check the selector in the inspector and load the script with defer or as a module.

Is getElementById faster than querySelector?

Slightly, but the difference rarely matters. Choose the one that makes your code clearer.

Summary

The DOM is the browser's live tree of objects built from your HTML, and JavaScript changes the page by changing that tree. Select elements with querySelector and querySelectorAll, update them with textContent, attributes and classList, and add or remove nodes with createElement, append and remove. Prefer textContent over innerHTML for anything that comes from users, and run scripts after the elements exist.

Related Articles

  • How Do Functions and Scope Work in JavaScript?
  • How Do Events and Event Listeners Work in JavaScript?
  • Learn Web Development: Complete Roadmap for Beginners
Share

Similar posts

  • How Do You Fetch Data from an API with the Fetch API?
  • What Are Promises and Async Await in JavaScript?
  • How Do Events and Event Listeners Work in JavaScript?
  • How Do Functions and Scope Work in JavaScript?
  • What Are Variables, Data Types and Operators in JavaScript?
  • What Is JavaScript and What Role Does It Play on the Web?
  • How to Start Web Development: A Beginner Roadmap
  • How Do You Make a Website Accessible for All Users?