Sujay Kundu

DOM Traversal and Manipulation using Javascript

Before DOM manipulation, it’s important to understand How to traverse the DOM

Traversing the DOM in JS

Traversing and manipulating the DOM (Document Object Model) without using any JavaScript frameworks or libraries like Angular or React involves working directly with the native DOM API. This can be done using HTML, CSS, and vanilla JavaScript. Here’s a detailed guide on how to traverse and manipulate the DOM using various event listeners and DOM manipulation methods:

  1. Basic DOM Traversal:
    To start, you need to understand how to access elements in the DOM and traverse through their structure.
  • getElementById: Retrieve an element by its unique ID.
<div id=“myElement”></div>

// javascript
const element = document.getElementById(“myElement”);
  • getElementsByClassName: – Retrieve a collection of elements with the same class name.
<div class=“myClass”></div>
<div class=“myClass”></div>

// javascript
const elements = document.getElementsByClassName(“myClass”);
  • getElementsByTagName: – Retrieve a collection of elements by their tag name.

// javascript
const paragraphs = document.getElementsByTagName(“p”);
  • querySelector: – Retrieve the first element that matches a given CSS selector.
const element = document.querySelector(“.myClass”);
  • querySelectorAll: – Retrieve a collection of elements that match a given CSS selector.
const elements = document.querySelectorAll(“.myClass”);

Dom Manipulation

DOM manipulation involves modifying elements, attributes, and content.

  1. Creating Elements:
const newElement = document.createElement(“div”);
  1. Appending and Inserting Elements:
parentElement.appendChild(newElement); // Append to the end
parentElement.insertBefore(newElement, referenceElement); // Insert before another element
  1. Updating Attributes:
element.setAttribute(“class”, “newClass”);
  1. Changing Content:
element.textContent = “New text content”;
element.innerHTML =<p>New HTML content</p>”;
  1. Removing Elements:

Common DOM Events:

Here are a few common DOM events you might encounter:

  • click: Triggered when an element is clicked.
  • submit: Triggered when a form is submitted.
  • keydown/keyup: Triggered when a keyboard key is pressed/released.
  • mouseover/mouseout: Triggered when the mouse pointer enters/leaves an element.
  • change: Triggered when the value of an input element changes.

Check if document has loaded
So there are multiple ways to check if the document is loaded

  1. readyStateChange

The Document.readyState property describes the loading state of the document . When the value of this property changes, a readystatechange event fires on the document object.

// using event handler
document.addEventListener(“readyStateChange”, (event) => {
    if ( == 'loading') {
      console.log('document is loading...')
    if ( == 'interactive') {
  } else if ( === “complete”) {
        console.log('page loaded')

// or
document.onreadystatechange = () => {
  if (document.readyState == "loading") {
  if (document.readyState == "interactive") {
  if (document.readyState == "complete") {
    // init app
  1. Using ‘DOMContentLoaded’ event

 document.addEventListener("DOMContentLoaded", (event) => {
   console.log("DOM fully loaded and parsed");
  1. Using Window load
window.addEventListener("load", (event) => {
  console.log("page loaded");


window.onload = function () {
 console.log('page is loaded...'