JavaScript & DOM Manipulation
Understanding Core DOM Concepts
with Examples
Introduction to JavaScript
• JavaScript is a scripting language for web development.
• It adds interactivity and dynamic behavior to web pages.
• Works with the Document Object Model (DOM) to update content in real
time.
What is DOM?
• DOM = Document Object Model.
• Represents HTML structure as a tree of objects.
• Allows JavaScript to access and modify webpage content dynamically.
Accessing Elements
• Use methods like getElementById(), getElementsByClassName(),
getElementsByTagName().
• Modern approach: querySelector() and querySelectorAll().
• Example: [Link]('#demo').innerText = 'Hello';
Changing Content
• innerText – modifies visible text only.
• innerHTML – allows adding HTML tags dynamically.
• textContent – changes text exactly as it appears in code.
Changing Styles
• Access style property to change CSS dynamically.
• Example: [Link] = 'red';
• Useful for animations, hover effects, and themes.
Creating Elements
• Use [Link]() to make new elements.
• Attach them using appendChild() or append().
• Example: let div = [Link]('div');
[Link](div);
Appending / Removing Elements
• appendChild() – adds an element as the last child.
• removeChild() – removes a specific element.
• append() – adds multiple elements or text nodes easily.
Event Handling
• Use addEventListener() for user interactions.
• Example: [Link]('click', myFunction);
• Supports multiple event types: click, input, hover, keydown, etc.
Traversing the DOM
• Move between nodes using parentNode, childNodes, nextSibling,
previousSibling.
• Example: [Link] = 'blue';
• Helps navigate and manipulate complex structures.
Attributes & ClassList
• getAttribute(), setAttribute(), removeAttribute() for HTML attributes.
• classList methods: add(), remove(), toggle(), contains().
• Example: [Link]('active');