Skip to main content

Command Palette

Search for a command to run...

DOM Elements and Manipulation in JavaScript (Beginner-Friendly Guide)

A beginner-friendly guide to understanding DOM elements, DOM tree structure, and JavaScript DOM manipulation step by step.

Updated
5 min read
DOM Elements and Manipulation in JavaScript (Beginner-Friendly Guide)

When learning web development, one of the most important concepts is the DOM (Document Object Model). It is the bridge between HTML and JavaScript, allowing JavaScript to read, modify, and control a web page.

Think of the DOM as the control panel of a website.


1. What is the DOM?

The Document Object Model (DOM) is a programming interface that represents an HTML document as a tree structure of objects.

JavaScript uses this structure to access and manipulate HTML elements.

Simple Analogy

Imagine a family tree.

Grandparent
   |
 Parent
   |
Child

Similarly, in HTML:

html
 └── body
      ├── h1
      ├── p
      └── button

Each element becomes a node in the DOM tree.

JavaScript can:

  • access these nodes

  • change their content

  • modify styles

  • add or remove elements

  • respond to user actions


2. Example HTML Page

Consider this simple HTML.

<!DOCTYPE html>
<html>
<head>
  <title>DOM Example</title>
</head>
<body>

  <h1 id="title">Hello World</h1>
  <p class="text">Learning DOM</p>
  <button>Click Me</button>

</body>
</html>

When the browser loads this page, it converts the HTML into a DOM tree.


3. How JavaScript Accesses the DOM

JavaScript accesses elements using the document object.

Think of document as the main entry gate to the webpage.

Example:

document.getElementById("title")

Here JavaScript searches the DOM tree and returns the element.


4. Selecting DOM Elements

Before manipulating elements, we must select them.

4.1 getElementById()

Selects element using id.

let heading = document.getElementById("title");
console.log(heading);

Analogy

This is like finding a person using their Aadhaar number — unique.


4.2 getElementsByClassName()

Selects elements using class name.

let texts = document.getElementsByClassName("text");

This returns a collection of elements.

Example:

console.log(texts[0]);

Analogy

Like finding all students wearing the same uniform.


4.3 getElementsByTagName()

Selects elements using HTML tag.

let paragraphs = document.getElementsByTagName("p");

This returns all <p> elements.


4.4 querySelector()

Selects the first matching element.

document.querySelector(".text")
document.querySelector("#title")
document.querySelector("p")

Analogy

Like searching Google and clicking the first result.


4.5 querySelectorAll()

Selects all matching elements.

document.querySelectorAll(".text")

Returns a NodeList.

Example:

let items = document.querySelectorAll(".text");

items.forEach(item => {
  console.log(item);
});

5. Manipulating DOM Elements

Once an element is selected, we can modify it.

Manipulation means changing content, styles, or structure.


6. Changing Text Content

textContent

Changes text inside an element.

let heading = document.getElementById("title");

heading.textContent = "Welcome to JavaScript";

Analogy

Like changing the name on a shop signboard.


innerHTML

Allows inserting HTML inside an element.

heading.innerHTML = "<span>New Title</span>";

Difference:

Property Purpose
textContent Only text
innerHTML Text + HTML

7. Changing Styles

JavaScript can modify CSS styles.

let title = document.getElementById("title");

title.style.color = "red";
title.style.fontSize = "40px";

Analogy

Like repainting a wall in your house.


8. Adding and Removing Classes

Instead of modifying styles directly, we often use CSS classes.

element.classList.add("active");
element.classList.remove("active");
element.classList.toggle("active");

Example:

let title = document.getElementById("title");

title.classList.add("highlight");

Analogy

Like putting a badge on someone’s shirt.


9. Creating New Elements

JavaScript can create HTML elements dynamically.

let newPara = document.createElement("p");

newPara.textContent = "This is a new paragraph";

But this element is not yet visible.


10. Adding Elements to the DOM

We must attach the element to the DOM tree.

document.body.appendChild(newPara);

Analogy

Creating a chair in a factory is not enough.

You must place it in the room for people to use.


11. Removing Elements

Elements can also be removed.

let element = document.getElementById("title");

element.remove();

12. Event Handling

DOM becomes powerful when it responds to user actions.

Events include:

  • click

  • hover

  • keypress

  • submit

Example:

let button = document.querySelector("button");

button.addEventListener("click", function(){
    alert("Button Clicked!");
});

Analogy

Like a doorbell.

When someone presses the bell (event), something happens (function runs).


13. Example: DOM Manipulation Program

HTML

<h1 id="title">Hello</h1>
<button id="btn">Change Text</button>

JavaScript

let button = document.getElementById("btn");
let title = document.getElementById("title");

button.addEventListener("click", function(){
    title.textContent = "DOM is Awesome!";
});

Result:

When the button is clicked, the text changes.


14. Why DOM Manipulation is Important

DOM manipulation allows developers to:

  • build interactive websites

  • update content dynamically

  • respond to user input

  • create dynamic UI

Examples:

  • Like button on Instagram

  • Live search results

  • Form validation

  • Dark mode toggle

All these rely on DOM manipulation.


15. Summary

DOM is the structure of the webpage that JavaScript can control.

Key concepts:

Concept Purpose
DOM Representation of HTML
document Entry point to DOM
getElementById Select element by id
querySelector Select first matching element
textContent Change text
innerHTML Insert HTML
style Modify CSS
createElement Create new element
appendChild Add element
remove Delete element
addEventListener Handle events

Final Analogy

Think of a webpage like a house.

Part Example
HTML Structure of the house
CSS Decoration and design
DOM Blueprint the browser creates
JavaScript The person who rearranges furniture

JavaScript uses the DOM blueprint to control the house.