DOM Introduction

📄 What is the DOM?

The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content.

  • Tree-like structure of nodes and objects
  • Platform and language-neutral interface
  • Allows JavaScript to interact with HTML
  • Dynamic content modification
DOM Tree Visualization:
Document (Root)
├── html
│   ├── head
│   │   ├── title
│   └── body
│       └── div#container
│           ├── h1#main-heading
│           ├── p.description
│           ├── ul#list
│           │   ├── li.item
│           │   ├── li.item
│           │   └── li.item
│           └── button#demo-button
Sample HTML Structure:

🔍 Accessing DOM Elements

JavaScript provides several methods to select and access elements in the DOM. Each method returns different types of collections or single elements.

JavaScript Editor
Selection Methods:
  • getElementById() - Returns single element
  • getElementsByClassName() - Returns HTMLCollection
  • getElementsByTagName() - Returns HTMLCollection
  • querySelector() - Returns first matching element
  • querySelectorAll() - Returns NodeList
Key Differences:
  • HTMLCollection: Live collection, array-like
  • NodeList: Static collection (usually), has forEach
  • querySelector accepts CSS selectors
  • ID selectors are fastest

🏷️ Element Properties & Attributes

DOM elements have properties and attributes that describe their state and characteristics. Properties are JavaScript object properties, while attributes are HTML attributes.

JavaScript Editor
Common Properties:
  • id - Element ID
  • className - CSS class
  • tagName - Tag name
  • textContent - Text inside
  • innerHTML - HTML content
Attribute Methods:
  • getAttribute() - Get attribute value
  • setAttribute() - Set attribute value
  • hasAttribute() - Check if exists
  • removeAttribute() - Remove attribute
Important Notes:
  • innerHTML parses HTML
  • textContent is plain text
  • Use classList for classes
  • Attributes are strings

🌳 Navigating the DOM Tree

You can navigate through the DOM tree using parent, child, and sibling relationships. This allows you to move from one element to related elements.

JavaScript Editor
Parent Navigation:
  • parentNode - Parent node
  • parentElement - Parent element
Child Navigation:
  • children - Child elements
  • firstChild - First child node
  • firstElementChild - First child element
  • childNodes - All child nodes
Sibling Navigation:
  • nextSibling - Next sibling node
  • nextElementSibling - Next element
  • previousSibling - Previous node
  • previousElementSibling - Previous element

✨ Interactive DOM Manipulation

Now let's see DOM manipulation in action! This example creates, modifies, and removes elements dynamically.

JavaScript Editor
💪 Practice Exercises:
  1. Create a function that adds a new list item
  2. Make a toggle function that shows/hides elements
  3. Create a counter that increments when button is clicked
  4. Build a simple to-do list with add/remove functionality

📋 DOM Quick Reference

Node Types:
  • 1 - ELEMENT_NODE
  • 3 - TEXT_NODE
  • 8 - COMMENT_NODE
  • 9 - DOCUMENT_NODE
Creation Methods:
  • createElement() - Create element
  • createTextNode() - Create text node
  • createDocumentFragment() - Lightweight container
Insertion Methods:
  • appendChild() - Add to end
  • insertBefore() - Insert before reference
  • replaceChild() - Replace element
  • cloneNode() - Clone element