Introduction to DOM Manipulation in JavaScript
Introduction to DOM Manipulation in JavaScript
DOM Manipulation
in JavaScript
Milind Gupta
Frontend Developer
Introduction to DOM Manipulation
What is DOM?
The Document Object Model (DOM) represents the structure of your
HTML document as a tree, where each element is a node. JavaScript
allows you to interact with and modify this structure dynamically.
Milind Gupta
Frontend Developer
Accessing DOM Elements
Methods to Access Elements:
getElementById(): Select by ID.
getElementsByClassName(): Select by class name (returns a collection).
getElementsByTagName(): Select by tag name (returns a collection).
querySelector() and querySelectorAll(): More versatile for selecting by
CSS selectors.
Example:
Milind Gupta
Frontend Developer
Manipulating DOM Elements
Change content:
innerHTML: Modifies HTML content.
textContent: Updates text without interpreting HTML.
Modify attributes:
setAttribute(), getAttribute(), removeAttribute().
Update styles using style property.
Example:
Milind Gupta
Frontend Developer
Adding and Removing DOM Elements
Adding Elements: Use createElement() and appendChild().
Removing Elements: Use removeChild() or element.remove().
Example:
Milind Gupta
Frontend Developer
Event Handling
Attach events using addEventListener.
Common events: click, mouseover, keydown, etc.
Example:
Milind Gupta
Frontend Developer
Found this
helpful?
Follow for
more
Milind Gupta