HOWTO · JavaScript
How to Change CSS Classes in JavaScript
Learn how to change an element's CSS classes with className and classList, including a click-driven example.
On this page
To change an element’s appearance with JavaScript, select it and update its classes. Assign className when replacing the entire class attribute; use classList to add, remove, or replace individual class tokens while preserving other classes. A click listener can make the change in response to a reader’s action.
Various Ways of Changing CSS Class in JavaScript
The complete page below gives the div an ID of dimensions and uses CSS classes to control its size and background. The initial bg_1 class sets a crimson background. Clicking the element removes that class and adds bg_2, which sets a teal background. You can save the page as an HTML file and open it in a browser to see the transition.
Using the className Property
className is a property, not a method. Assigning a string to it replaces the element’s complete class attribute. For example, setting it to "dimensions bg_2" changes the background and keeps the sizing class. Setting it only to "bg_2" would also remove dimensions, so include every class the element should retain. This approach is useful when you deliberately want to define the full class set, such as applying a known group of state classes at once.
Using classList.remove and classList.add
classList is a property that exposes methods for editing individual class tokens. Call classList.remove("bg_1") and classList.add("bg_2") to change the background classes without disturbing dimensions or other unrelated classes. add does not duplicate a token that is already present, and remove only affects the named token. Use these operations when an element has classes that should remain in place, including classes added by other page behavior.
Using the classList.replace() Method
classList.replace("bg_1", "bg_2") replaces one token with another and leaves other classes intact. The replacement happens only when bg_1 is present; when the old token is absent, no token is changed. Choose this method when one class state maps directly to another. Separate remove and add calls are useful when you want to make the two operations explicit or when the handler has other work to perform between them.
Here is a complete HTML page, including its styles, element, and click handler. The small JavaScript adapter at the end models the same page interaction so the example also has a deterministic console result; in a browser, the inline page script handles the actual click.
const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Change a CSS class</title>
<style>
.dimensions { width: 200px; height: 100px; }
.bg_1 { background-color: crimson; }
.bg_2 { background-color: teal; }
</style>
</head>
<body>
<div id="dimensions" class="dimensions bg_1">Click this box</div>
<script>
const element = document.getElementById("dimensions");
element.addEventListener("click", () => {
element.classList.remove("bg_1");
element.classList.add("bg_2");
});
</script>
</body>
</html>`;
const element = {
className: "dimensions bg_1",
addEventListener(type, handler) {
this.handler = handler;
},
classList: {
remove(token) {
element.className = element.className
.split(" ")
.filter((name) => name !== token)
.join(" ");
},
add(token) {
const names = element.className.split(" ");
if (!names.includes(token)) names.push(token);
element.className = names.join(" ");
}
}
};
const document = { getElementById: () => element };
const selected = document.getElementById("dimensions");
selected.addEventListener("click", () => {
selected.classList.remove("bg_1");
selected.classList.add("bg_2");
});
selected.handler();
console.log(selected.className);
dimensions bg_2
The browser portion shows the real DOM pattern: document.getElementById("dimensions") returns the element, and addEventListener registers the callback that runs on a click. The Node.js adapter reproduces the class-token change for the console output, since Node.js does not provide a page’s DOM by default. The stylesheet determines the visible colors; changing a class name alone has no visual effect unless a matching CSS rule exists. For a direct full replacement, use className and list every class to retain. For a targeted change that leaves sizing, layout, and unrelated state classes untouched, use classList methods. If your handler seems to do nothing, check that the ID matches and that both background classes are defined in the page’s stylesheet.