Skip to main content

Command Palette

Search for a command to run...

DOM Manipulation in Javascript - How it Works Behind the Scenes

Javascript DOM Manipulation Behind the Scenes

Updated
4 min read
DOM Manipulation in Javascript - How it Works Behind the Scenes
R

I’m an curious and aspiring MERN stack developer who loves building web apps that can actually solve a real world problem with scaleability, technical feasibility . I mostly work on frontend projects with React and Next.js, also have explored backend with Express and Node.js with cloud databased like MongoDB Atlas. Contributed in several open source projects during Hacktoberfest 2025. Very keen towards the idea of technology (AI and ML) can change the world we`re currently in!

DOM Manipulation in Javascript is a very fundamental topic while deep diving in js.

To Understand DOM , first we should know what is actually DOM?

DOM stands for Document Object Model

DOM is a tree-like representation of the HTML page,where every element like <div> <p> <h1> <button> etc are a node in the tree.

If you have not explored data structure just yet and wondering what`s a tree? Here is what you need to understand before learning DOM and its manipulation.

Understanding Trees and Nodes in JavaScript (DOM Context)

What is a Tree?

A tree is a data structure that organizes data in a hierarchical way.

  • It starts from a root node (the top element).

  • Each node can have child nodes, forming branches.

  • Nodes without children are called leaf nodes.

Think of it like a family tree:

  • Root = Grandparent

  • Children = Parents

  • Leaves = Kids with no further children

What is a Node?

A node is an individual element in the tree. In the DOM:

  • Every HTML element is a node: <div>, <p>, <h1>

  • Text inside elements is also a text node.

  • Attributes like id or class can be considered property nodes in some contexts.

Example: Simple HTML

<body>
  <h1>Hello</h1>
  <p>Welcome to my page</p>
</body>

DOM Tree Representation:

        body
       /    \
     h1      p
   "Hello" "Welcome to my page"
  • body → root node

  • h1 and p → child nodes of body

  • "Hello" and "Welcome to my page" → text nodes (leaf nodes)

Why This Matters

  • When you use JavaScript to manipulate the DOM, you are essentially navigating this tree.

  • Functions like document.querySelector() or appendChild() work by finding and modifying nodes.

  • Understanding this tree structure helps you write efficient code and avoid unnecessary DOM operations (which can slow down your page).

Understanding DOM and DOM Manipulation in JavaScript

What is the DOM?

The DOM (Document Object Model) is a tree-like representation of your HTML page.

  • It allows JavaScript to interact with HTML elements: read them, change them, or respond to user actions.

  • Think of the DOM as a bridge between your web page (HTML) and JavaScript.

Example HTML & DOM Tree Representation

<body>
  <h1>Welcome to My Page</h1>
  <p id="intro">This is a simple paragraph.</p>
  <button id="btn">Click Me</button>
</body>
DOM Tree Representaion :-
        body
       /   |   \
     h1    p    button  "Welcome" "This is a simple paragraph." "Click Me"

What is DOM Manipulation?

DOM Manipulation means using JavaScript to change the DOM tree. This can include:

  • Changing text or HTML content

  • Modifying attributes (like id, class, src)

  • Adding or removing elements

  • Listening and responding to user events

Example 1: Changing Text Content

const paragraph = document.querySelector('#intro');
paragraph.textContent = 'DOM manipulation is fun!';
  • querySelector → finds the node in the DOM tree

  • textContent → changes the text inside the node

    Example 2: Adding a New Element

      const btn = document.querySelector('#btn');
    
      btn.addEventListener('click', () => {
        const newPara = document.createElement('p'); // Create a new node
        newPara.textContent = 'You clicked the button!';
        document.body.appendChild(newPara); // Add it to the DOM tree
      });
    
    • createElement → creates a new node in memory

    • appendChild → inserts the node into the DOM tree, making it visible.

      Common DOM Methods and Properties for Manipulation

    • 1. document.getElementById() Selects a single element by its id.

  • Behind the scenes: Browser searches the DOM tree for the node with matching id.

  • 2. document.querySelector()

    • Selects the first element that matches a CSS selector.
  • const firstBtn = document.querySelector('.btn'); firstBtn.style.backgroundColor = 'blue';

  • . document.querySelectorAll()

    • Selects all elements that match a CSS selector, returns a NodeList.

    • const buttons = document.querySelectorAll('.btn'); buttons.forEach(btn => btn.style.color = 'white');

    • Can loop over elements using forEach.

    • 4.element.textContent

      • Reads or updates the text content of an element.
            const para = document.querySelector('p');
            para.textContent = 'New paragraph content';

How DOM Manipulation Works Behind the Scenes

  • HTML Parsing → Browser reads HTML → builds DOM tree

  • CSS Parsing → Browser builds CSSOM (CSS Object Model)

  • JavaScript Execution → JS modifies DOM nodes, adds/removes elements, listens to events

  • Rendering Engine → Updates the page visually by repainting or reflowing

    Behind the Scenes of Example 2:

    • querySelector('#btn') → Browser searches DOM tree for button node

    • createElement('p') → Browser allocates memory for a new node

    • appendChild(newPara) → Browser inserts node into DOM → triggers reflow/repaint

    • Event listener is stored in event table, waits for user interaction

      Reflow vs Repaint:

      • Reflow: Browser recalculates the layout of part or all of the page (expensive)

      • Repaint: Browser updates the pixels for styling changes without changing layout (less expensive)

        Why Understanding DOM Manipulation is Important

        • Writing efficient code → Avoid unnecessary reflows/repaints

        • Building interactive web apps → Buttons, forms, dynamic content

        • Understanding the tree structure of the DOM makes debugging easier

Mini Project Idea

  • Show real-world usage of DOM selection , node creation and appending.
A
aashuu ✦6mo ago

👍