Modifying Elements

Change anything on the page. Content, style, structure — all in your control.

Content

Change what an element displays.

const el = document.querySelector(".card");

// textContent — plain text (SAFE):
el.textContent = "Hello, World!";
el.textContent; // reads all text, ignores HTML tags

// innerHTML — parses HTML (DANGEROUS with user input):
el.innerHTML = "<strong>Bold</strong> text";
el.innerHTML; // returns HTML string including tags

// innerText — visible text only (respects CSS):
el.innerText; // ignores hidden elements, adds newlines

// outerHTML — includes the element itself:
el.outerHTML; // "<div class='card'><strong>Bold</strong> text</div>"

textContent ✓

Safe for user input

No XSS risk

innerHTML ⚠️

XSS risk with user data

Never use unsanitized input

Attributes

Read and write element attributes.

const link = document.querySelector("a");

// Standard attributes map to properties:
link.href;          // full URL
link.id;            // id attribute
link.className;     // class attribute (string)

// getAttribute / setAttribute for any attribute:
link.getAttribute("href");       // exactly as written in HTML
link.setAttribute("target", "_blank");
link.removeAttribute("title");
link.hasAttribute("rel");        // true/false

// data-* attributes (custom data):
// <div data-user-id="42" data-role="admin">
const el = document.querySelector("[data-user-id]");
el.dataset.userId;   // "42" (camelCase!)
el.dataset.role;     // "admin"
el.dataset.newProp = "value"; // adds data-new-prop

Classes & Styles

Toggle appearance without touching CSS files.

const el = document.querySelector(".card");

// classList (preferred for classes):
el.classList.add("active", "visible");    // add one or more
el.classList.remove("hidden");            // remove
el.classList.toggle("open");              // add if missing, remove if present
el.classList.toggle("dark", isDark);      // force add/remove
el.classList.contains("active");          // true/false
el.classList.replace("old", "new");       // swap

// Inline styles:
el.style.color = "red";
el.style.backgroundColor = "blue";       // camelCase!
el.style.transform = "rotate(45deg)";
el.style.cssText = "color: red; font-size: 16px"; // all at once

// Read computed (final) styles:
const styles = getComputedStyle(el);
styles.color;      // "rgb(255, 0, 0)"
styles.fontSize;   // "16px"

💡 Prefer toggling CSS classes over inline styles. Classes are easier to maintain and can leverage CSS transitions.

Creating Elements

Build new DOM nodes from scratch.

// Create an element:
const card = document.createElement("div");
card.className = "card";
card.textContent = "New card!";

// Create text node:
const text = document.createTextNode("Hello");

// At this point, card exists in memory
// but is NOT in the page yet!

createElement makes a new element in memory. It's not visible until you insert it into the DOM.

1 / 3

Insert & Remove

Place elements in the DOM or take them out.

const parent = document.querySelector(".list");
const newEl = document.createElement("li");
newEl.textContent = "New item";

// Append (add as last child):
parent.appendChild(newEl);
parent.append(newEl, "text", anotherEl); // multiple!

// Prepend (add as first child):
parent.prepend(newEl);

// Insert relative to an element:
const ref = document.querySelector(".target");
ref.before(newEl);    // insert before target
ref.after(newEl);     // insert after target

// Insert at specific position:
parent.insertBefore(newEl, ref); // before ref

// Replace:
parent.replaceChild(newEl, oldEl);
oldEl.replaceWith(newEl); // modern

// Remove:
newEl.remove();                    // modern
parent.removeChild(newEl);         // legacy

// Clone:
const clone = newEl.cloneNode(true);  // deep (with children)
const shallow = newEl.cloneNode(false); // shallow (no children)

FAQ

Common questions about modifying elements.