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.
Selection Methods:
getElementById()- Returns single elementgetElementsByClassName()- Returns HTMLCollectiongetElementsByTagName()- Returns HTMLCollectionquerySelector()- Returns first matching elementquerySelectorAll()- 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.
Common Properties:
id- Element IDclassName- CSS classtagName- Tag nametextContent- Text insideinnerHTML- HTML content
Attribute Methods:
getAttribute()- Get attribute valuesetAttribute()- Set attribute valuehasAttribute()- Check if existsremoveAttribute()- Remove attribute
Important Notes:
innerHTMLparses HTMLtextContentis plain text- Use
classListfor 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.
Parent Navigation:
parentNode- Parent nodeparentElement- Parent element
Child Navigation:
children- Child elementsfirstChild- First child nodefirstElementChild- First child elementchildNodes- All child nodes
Sibling Navigation:
nextSibling- Next sibling nodenextElementSibling- Next elementpreviousSibling- Previous nodepreviousElementSibling- Previous element
✨ Interactive DOM Manipulation
Now let's see DOM manipulation in action! This example creates, modifies, and removes elements dynamically.
💪 Practice Exercises:
- Create a function that adds a new list item
- Make a toggle function that shows/hides elements
- Create a counter that increments when button is clicked
- 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 elementcreateTextNode()- Create text nodecreateDocumentFragment()- Lightweight container
Insertion Methods:
appendChild()- Add to endinsertBefore()- Insert before referencereplaceChild()- Replace elementcloneNode()- Clone element