Skip to main content

DOM Manipulation

The Document Object Model (DOM) is the browser's representation of your HTML page as a tree of objects. JavaScript can read and modify this tree, which is how you make web pages interactive.

Selecting Elements

getElementById

Select a single element by its id attribute:

const header = document.getElementById("main-header");
console.log(header.textContent);

querySelector and querySelectorAll

Select elements using CSS selectors — the most versatile approach:

Ctrl+Enter
HTML
CSS
JS
Preview

Comparison of Selection Methods

MethodReturnsUse When
getElementByIdElementYou have a unique ID
querySelectorElementYou need the first CSS match
querySelectorAllNodeListYou need all matching elements
getElementsByClassNameHTMLCollectionLegacy code (prefer querySelector)

Reading and Modifying Content

textContent vs innerHTML

<div id="output">
  <strong>Hello</strong> World
</div>
Ctrl+Enter
HTML
CSS
JS
Preview

Always use textContent when inserting user-generated content to prevent cross-site scripting (XSS) attacks.

Modifying Styles and Classes

Working with Classes

Ctrl+Enter
HTML
CSS
JS
Preview

Inline Styles

const box = document.querySelector(".box");
box.style.backgroundColor = "#E21B1B";
box.style.padding = "1rem";
box.style.display = "none";

Prefer toggling CSS classes over setting inline styles — it keeps your JavaScript clean and your styles in CSS where they belong.

Modifying Attributes

Ctrl+Enter
HTML
CSS
JS
Preview

Creating and Inserting Elements

createElement

Ctrl+Enter
HTML
CSS
JS
Preview

Building Multiple Elements

Ctrl+Enter
HTML
CSS
JS
Preview

Using a DocumentFragment is more efficient than appending elements one by one because it causes only a single reflow.

insertAdjacentHTML

A convenient way to insert HTML at specific positions:

Ctrl+Enter
HTML
CSS
JS
Preview

Removing Elements

Ctrl+Enter
HTML
CSS
JS
Preview

Event Handling

Events are how you respond to user interactions.

addEventListener

Ctrl+Enter
HTML
CSS
JS
Preview

Common Events

EventFires When
clickElement is clicked
inputInput value changes
submitForm is submitted
keydownKey is pressed
mouseoverCursor enters an element
focusElement gains focus
blurElement loses focus
loadPage or resource finishes loading

Event Delegation

Instead of adding listeners to many child elements, add one listener to a parent:

Ctrl+Enter
HTML
CSS
JS
Preview

Event delegation is more memory-efficient and works for dynamically added elements.

Preventing Default Behavior

Ctrl+Enter
HTML
CSS
JS
Preview

Practical Exercise

Build an interactive to-do list:

<input id="todo-input" type="text" placeholder="Add a task..." />
<button id="add-btn">Add</button>
<ul id="todo-list"></ul>
Ctrl+Enter
HTML
CSS
JS
Preview

Key Takeaways

  • Use querySelector and querySelectorAll for selecting elements — they accept any CSS selector.
  • Prefer textContent over innerHTML when inserting user-generated content to prevent XSS.
  • Toggle CSS classes instead of setting inline styles for cleaner separation of concerns.
  • Use DocumentFragment when inserting many elements to minimize reflows.
  • Event delegation on a parent element is more efficient than individual listeners on each child.