How do you change the content of an element with JavaScript?
There are three main levers, and choosing the right one is most of the skill. The textContent property sets the text inside an element and always treats the value as plain text. The innerHTML property takes a string of markup and parses it. The classList property adds and removes class names, which is how you change appearance without touching styles directly.
// The DOM only exists in a browser, so check before using it.
if (typeof document !== "undefined") {
const card = document.querySelector(".card");
// Plain text - always safe.
card.textContent = "Welcome back";
// Markup - parsed as HTML.
card.innerHTML = "<strong>Welcome back</strong>";
// Classes drive the styling.
card.classList.add("is-active");
card.classList.remove("is-loading");
console.log(card.classList.contains("is-active")); // true
// Attributes for everything else.
card.setAttribute("aria-live", "polite");
} else {
console.log("No DOM in this runtime - open a browser console to try it.");
}The security rule is the one to remember. Setting innerHTML with a string that contains user input lets the browser parse it as markup, so a value containing a script tag becomes a cross-site scripting hole. Setting textContent never has that problem, because the value can only ever be text. If you need markup from an untrusted source, sanitise it first.
There is a performance difference too. Assigning to innerHTML parses a whole new subtree and throws away the old one, which means any event listeners on the old children are lost and any references you held are stale. Setting textContent on a leaf node does not have that cost.
For styling, the style property gives you access to individual declarations and the cssText property replaces the entire declaration block. The classList property is usually better than either, because it keeps the decision about what something looks like in the stylesheet where it belongs.