Modifying Elements
Once you can select DOM elements, it’s time to update their content, attributes, and styling. Today you’ll manipulate text, HTML, classes, inline styles, and even create new nodes on the fly.
Step 1: Updating Text
const heading = document.querySelector("h1");
heading.textContent = "Welcome back!";textContentreplaces all text inside the element.- It treats content as plain text—HTML tags are escaped.
Step 2: Inserting HTML
const hero = document.querySelector(".hero");
hero.innerHTML = "<strong>New message</strong> for learners!";innerHTMLparses the string as HTML. Use it carefully to avoid XSS vulnerabilities if content comes from users.- Prefer DOM methods (
createElement,append) when building complex structures programmatically.
Step 3: Appending and Prepending Content
const list = document.querySelector(".tasks");
const li = document.createElement("li");
li.textContent = "Review iteration methods";
list.append(li); // adds to end
const first = document.createElement("li");
first.textContent = "Check notifications";
list.prepend(first); // adds to startappendandprependaccept nodes or strings.appendChildis similar but only accepts nodes.
Step 4: Removing Elements
const banner = document.querySelector(".banner");
banner.remove();remove()deletes the node from its parent.- Alternatively, call
parent.removeChild(child)for older browser support.
Step 5: Working with Attributes
const link = document.querySelector("a.cta");
link.setAttribute("href", "https://devsteps.io");
link.setAttribute("target", "_blank");
console.log(link.getAttribute("href"));setAttribute,getAttribute, andremoveAttributeprovide generic access.- For common attributes, you can use properties (
link.href,link.id) as well.
Step 6: Managing Classes
const card = document.querySelector(".card");
card.classList.add("highlight");
card.classList.remove("hidden");
card.classList.toggle("active");
card.classList.replace("old", "new");
console.log(card.classList.contains("active"));classListoffers convenient methods for class manipulation.toggle("active", condition)lets you add/remove based on a boolean.
Step 7: Inline Styles
const alertBar = document.querySelector(".alert");
alertBar.style.backgroundColor = "#2563eb";
alertBar.style.color = "#fff";
alertBar.style.padding = "12px";- Use camelCase property names (e.g.,
backgroundColor). - Inline styles override stylesheets; prefer CSS classes for consistent themes.
Step 8: CSS Custom Properties
document.documentElement.style.setProperty("--accent", "#f97316");- Set global CSS variables via
setPropertyonstyle. - Retrieve with
getComputedStyle(document.documentElement).getPropertyValue("--accent").
Step 9: Practice Prompts
- Replace the text inside an element with ID
statusto"All caught up!". - Append a new
<li>item labeled"Book review"to a list with class.reading-list. - Toggle a
"dark-mode"class onbodywhen a checkbox is checked. - Update an image’s
srcandaltattributes to display a new illustration. - Set a CSS custom property
--primaryand apply it to highlight a banner.
Key Takeaways
- ✅
textContentfor safe text,innerHTMLfor HTML snippets (with caution). - ✅ Create nodes with
document.createElementand insert them viaappend,prepend, orappendChild. - ✅ Manage classes with
classListand attributes withsetAttribute/getAttribute. - ✅ Inline styles and CSS variables let you tweak presentation dynamically.
🎯 Quick Check
- When should you prefer
textContentoverinnerHTML? - How do
appendandappendChilddiffer? - What does
classList.toggle("active")do? - How can you update a CSS variable from JavaScript?
Next lesson: respond to user actions by wiring up event listeners. 🖱️