DOM Manipulation โ€“ Complete Guide

What is DOM?

The Document Object Model (DOM) is a programming interface for web documents. It represents the page as a structured tree of objects that can be manipulated with JavaScript.

DOM Access Methods

Node Properties

Traversing the DOM

Creating and Modifying Elements

Attributes Manipulation

Class Manipulation

Style Manipulation

Event Handling

Event Object Properties:

Event Delegation

Attaching one event listener to a parent element to handle events from children using event.target. Efficient for dynamically added elements.

Forms & Input

Advanced Techniques

Performance Tips

Best Practices

Live Example


      const btn = document.querySelector("#clickMe");
      btn.addEventListener("click", () => {
        const newDiv = document.createElement("div");
        newDiv.textContent = "You clicked!";
        document.body.appendChild(newDiv);
      });
          

Debugging Tips

๐Ÿ“Œ JavaScript Event Listeners โ€“ Complete Breakdown

Event listeners allow JavaScript to "listen" for interactions or changes in the DOM. They enable dynamic, interactive websites.

MDN Docs: addEventListener()

๐Ÿง  Syntax:


      element.addEventListener("eventType", callbackFunction, useCapture);
        

๐Ÿ› ๏ธ Commonly Used Events & Examples:

1. Click Event


      document.querySelector("#btn").addEventListener("click", function() {
        alert("Button Clicked!");
      });
        

2. Mouse Events


      element.addEventListener("mouseover", () => {
        element.style.backgroundColor = "yellow";
      });
        

3. Keyboard Events


      document.addEventListener("keydown", function(e) {
        console.log("Key pressed:", e.key);
      });
        

4. Form Events


      document.querySelector("form").addEventListener("submit", function(e) {
        e.preventDefault(); // Prevents actual submission
        alert("Form submitted!");
      });
        

5. Window Events


      window.addEventListener("resize", () =$gt; {
        console.log("Window resized!");
      });
        

6. Touch Events (Mobile)

7. Custom Events


      const myEvent = new Event("customEvent");
      element.addEventListener("customEvent", () =$gt; console.log("Custom event fired!"));
      element.dispatchEvent(myEvent);
        

๐Ÿ“Œ Event Object

The callback function receives an event object with useful properties:

๐Ÿงญ Event Flow (Capture vs. Bubbling)

Events travel in three phases:

  1. Capture phase โ€“ Down the DOM tree
  2. Target phase โ€“ Event reaches target
  3. Bubbling phase โ€“ Bubbles back up the DOM tree (default)

To handle capturing:


      element.addEventListener("click", handler, true); // capture = true
        

๐Ÿงน Removing Event Listeners


      function logClick() {
        console.log("Clicked");
      }
      element.addEventListener("click", logClick);
      element.removeEventListener("click", logClick);
        

Note: You must reference the same function to remove it!

๐Ÿš€ Event Delegation

Attach a single listener to a parent and use event.target to handle child interactions.


      document.querySelector("#list").addEventListener("click", function(e) {
        if (e.target.tagName === "LI") {
          console.log("Clicked:", e.target.textContent);
        }
      });
        

โœ… Efficient for dynamic elements!

๐Ÿ“š More MDN Event Reference

โœ… Best Practices

This is a paragraph. Click the button to change this text.

This box changes color when you click the button.

Click the button to see an alert.

Click the button to highlight all paragraphs.

Click the button to add or remove an item.

This text can be toggled.