100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

JavaScript DOM Manipulation Cheat Sheet

JavaScript DOM Manipulation Cheat Sheet

Covers selecting DOM elements, creating and modifying nodes, handling events with delegation, and toggling classes with vanilla JavaScript.

1 PageBeginnerApr 10, 2026

Selecting Elements

Query the DOM with CSS selectors or legacy accessors.

javascript
document.getElementById("app");             // Single element by iddocument.querySelector(".card");             // First match (CSS selector)document.querySelectorAll(".card");          // NodeList of all matchesdocument.getElementsByClassName("card");     // Live HTMLCollectiondocument.getElementsByTagName("li");         // Live HTMLCollection// Iterating a NodeListdocument.querySelectorAll(".card").forEach(el => console.log(el));

Creating & Modifying Nodes

Build, insert, and remove elements.

javascript
const el = document.createElement("div");el.textContent = "Hello";          // Safe text (no HTML parsing)el.innerHTML = "<b>Hello</b>";     // Parses HTML (careful with user input)el.setAttribute("data-id", "42");el.id = "greeting";document.body.appendChild(el);       // Add as last childparent.insertBefore(el, referenceNode);el.remove();                         // Remove from the DOMconst clone = el.cloneNode(true);    // Deep clone (true = include children)

Event Handling

Listen for and delegate DOM events.

javascript
const btn = document.querySelector("#submit");btn.addEventListener("click", (event) => {  event.preventDefault();     // Stop default action (e.g. form submit)  console.log("clicked", event.target);});btn.removeEventListener("click", handlerFn);// Event delegation: listen on a parent, filter by targetdocument.querySelector("ul").addEventListener("click", (e) => {  if (e.target.matches("li")) {    console.log("item clicked:", e.target.textContent);  }});

Classes & Inline Styles

Toggle CSS classes and set styles from JavaScript.

javascript
const el = document.querySelector(".box");el.classList.add("active");el.classList.remove("hidden");el.classList.toggle("open");el.classList.contains("active");   // true/falseel.style.backgroundColor = "coral";el.style.setProperty("--gap", "8px");   // Set a CSS custom property

Quick Reference

Handy DOM properties and methods.

  • el.parentElement- Reference to the direct parent element
  • el.children- Live HTMLCollection of child elements (no text nodes)
  • el.closest('.card')- Nearest ancestor (including itself) matching a selector
  • el.dataset.id- Read/write a data-id attribute via the camelCased dataset API
  • el.getBoundingClientRect()- Returns size and position relative to the viewport
  • DOMContentLoaded- Fires when HTML is parsed, before images/styles finish loading
Pro Tip

Prefer textContent over innerHTML when inserting untrusted or dynamic strings — innerHTML parses its argument as HTML and is a common source of cross-site scripting (XSS) vulnerabilities.

Was this cheat sheet helpful?

Explore Topics

#JavaScriptDOMManipulation#JavaScriptDOMManipulationCheatSheet#Programming#Beginner#SelectingElements#CreatingModifyingNodes#EventHandling#ClassesInlineStyles#OOP#WebDevelopment#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet