Modifying Elements
After selecting or creating an element, JavaScript can modify it.
Common modifications include:
- changing text
- changing attributes
- changing classes
- changing styles
- adding or removing elements
- enabling or disabling controls
These changes update the live DOM and can affect what the user sees immediately.
Changing Text
Use textContent for plain text.
<p id="status">Loading...</p>const status = document.querySelector("#status");
status.textContent = "Ready";textContent treats the value as text, not HTML.
status.textContent = "<strong>Ready</strong>";The page displays the characters <strong>Ready</strong>.
This is usually safer for user-controlled content.
Changing HTML
Use innerHTML when you intentionally want to parse a string as HTML.
const message = document.querySelector("#message");
message.innerHTML = "<strong>Saved</strong>";Do not pass untrusted user input to innerHTML.
const userComment = getUserComment();
message.innerHTML = userComment; // riskyPrefer:
message.textContent = userComment;Changing Attributes
Attributes are values written in HTML.
<img id="avatar" src="default.png" alt="Default avatar">Use properties for many common attributes.
const avatar = document.querySelector("#avatar");
avatar.src = "asha.png";
avatar.alt = "Asha profile photo";You can also use setAttribute().
avatar.setAttribute("loading", "lazy");Read attributes with getAttribute().
console.log(avatar.getAttribute("alt"));Remove attributes with removeAttribute().
avatar.removeAttribute("loading");Boolean Attributes
Some attributes are boolean-like in HTML.
Examples:
disabledcheckedselectedhidden
Use properties when possible.
const saveButton = document.querySelector("#save");
saveButton.disabled = true;saveButton.disabled = false;Working with Classes
Classes are commonly used for styling and state.
Use classList.
const panel = document.querySelector(".panel");
panel.classList.add("open");
panel.classList.remove("closed");Toggle a class:
panel.classList.toggle("open");Check whether a class exists:
if (panel.classList.contains("open")) {
console.log("Panel is open");
}You can pass a second argument to toggle() to force the result.
const isValid = false;
panel.classList.toggle("error", !isValid);This adds error when !isValid is true and removes it when false.
Changing Inline Styles
Elements have a style property for inline styles.
const box = document.querySelector(".box");
box.style.backgroundColor = "tomato";
box.style.padding = "1rem";CSS property names use camelCase in JavaScript.
box.style.borderRadius = "8px";Inline styles are useful for dynamic values.
For normal UI states, classes are often better.
box.classList.add("warning");CSS:
.warning {
background-color: tomato;
padding: 1rem;
border-radius: 8px;
}Classes keep styling in CSS and behavior in JavaScript.
Dataset Attributes
data-* attributes store custom values in HTML.
<button data-product-id="42">Add to cart</button>Use dataset to read them.
const button = document.querySelector("button");
console.log(button.dataset.productId); // "42"Notice the conversion:
data-product-idin HTMLdataset.productIdin JavaScript
Dataset values are strings.
Convert them if you need numbers or booleans.
const productId = Number(button.dataset.productId);Removing Elements
Use remove() to remove an element from the DOM.
const alert = document.querySelector(".alert");
alert.remove();To clear all children from an element, use replaceChildren().
const list = document.querySelector("#tasks");
list.replaceChildren();You can also replace children with new nodes.
const emptyState = document.createElement("p");
emptyState.textContent = "No tasks yet.";
list.replaceChildren(emptyState);Updating Form Controls
Form controls have useful properties.
<input id="email" value="user@example.com">
<input id="subscribe" type="checkbox" checked>const email = document.querySelector("#email");
const subscribe = document.querySelector("#subscribe");
console.log(email.value);
console.log(subscribe.checked);Update them with properties.
email.value = "";
subscribe.checked = false;Reading Layout
Sometimes you need information about where an element is on the screen.
const card = document.querySelector(".card");
const rect = card.getBoundingClientRect();
console.log(rect.top);
console.log(rect.width);Be careful when mixing layout reads and writes repeatedly. That can force the browser to recalculate layout more often than needed.
Prefer grouping reads together and writes together in performance-sensitive code.
Best Practices
- Use
textContentfor text. - Use
classListfor visual states. - Use properties for common attributes like
disabled,value,checked,src, andalt. - Use
datasetfor simple custom metadata. - Avoid untrusted
innerHTML. - Keep DOM updates focused and easy to follow.
Common Mistakes
This changes HTML, not plain text:
output.innerHTML = userInput;Use:
output.textContent = userInput;Another mistake is overwriting all classes.
button.className = "active";This removes every existing class. Prefer classList when adding or removing one state.
button.classList.add("active");Summary
DOM modification is how browser JavaScript changes a page.
Use textContent, attributes, properties, classList, styles, and methods like remove() or replaceChildren() to update elements. Keep security in mind whenever a string is interpreted as HTML.