DOM Manipulation
Interact with the HTML Document Object Model to dynamically update web pages using JavaScript.
0 of 5 concepts completed (0%)
What You'll Learn
Tutorial Contents
1.Selecting & Modifying
// Selecting
const btn = document.querySelector("#submit-btn");
const inputs = document.querySelectorAll("input[type='text']");
const form = document.getElementById("login-form");// Traversal const parent = element.parentElement; const children = element.children; // HTMLCollection const siblings = [...element.parentElement.children].filter(c => c !== element); const next = element.nextElementSibling; const prev = element.previousElementSibling;
// Content el.textContent = "Safe text (no HTML parsing)"; el.innerHTML = "Bold"; // parses HTML el.innerText = "Visible text only";
// Attributes el.setAttribute("aria-expanded", "true"); el.getAttribute("data-id"); el.removeAttribute("disabled"); el.dataset.userId = "42"; // → data-user-id="42"
// Classes el.classList.add("active", "visible"); el.classList.remove("hidden"); el.classList.toggle("expanded"); el.classList.replace("btn-blue", "btn-red"); el.classList.contains("active"); // → boolean
// Styles el.style.display = "none"; el.style.cssText = "color: red; font-weight: bold;"; getComputedStyle(el).fontSize; // read computed value ```
2.Events & Event Delegation
// Add listeners
btn.addEventListener("click", handleClick);
form.addEventListener("submit", (e) => {
e.preventDefault();
// process form// Event object function handleClick(event) { console.log(event.target); // element clicked console.log(event.currentTarget); // element with listener console.log(event.type); // "click" event.stopPropagation(); // stop bubbling }
// Event delegation one listener for all items const list = document.querySelector("#todo-list"); list.addEventListener("click", (e) => { const item = e.target.closest("[data-id]"); if (!item) return; const id = item.dataset.id; if (e.target.matches(".delete-btn")) { item.remove(); } else if (e.target.matches(".complete-btn")) { item.classList.toggle("done"); } });
// Creating and inserting elements efficiently const fragment = document.createDocumentFragment(); const items = ["Apple", "Banana", "Cherry"];
items.forEach(text => { const li = document.createElement("li"); li.className = "list-item"; li.textContent = text; fragment.appendChild(li); });
document.querySelector("#list").appendChild(fragment); // one DOM operation ```
Code Example
// Select elements
const btn = document.querySelector("#myButton");
const list = document.getElementById("myList");
// Modify content
btn.textContent = "Click Me";
btn.classList.add("active");
// Event listener
btn.addEventListener("click", (e) => {
const li = document.createElement("li");
li.textContent = "New item";
list.appendChild(li);
});
// Event delegation
list.addEventListener("click", (e) => {
if (e.target.tagName === "LI") {
e.target.remove();
}
});Pro Tips
- 1Use event delegation attach one listener to a parent instead of dozens to children. It handles dynamically added elements automatically.
- 2Batch DOM reads before writes to prevent layout thrashing. Reading offsetHeight forces the browser to recalculate layout; mixing reads/writes causes it to recalculate repeatedly.
- 3DocumentFragment lets you build multiple nodes off-DOM and append once a 10x performance gain for inserting many elements.
- 4Use data-* attributes to link DOM elements to data instead of fragile CSS class or ID selectors.
Topics Covered
Continue Learning
Explore the official documentation and deepen your knowledge with real-world practice.
Explore the Full Portfolio
This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.
The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.
For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.