How to Use classList Toggle in JavaScript DOM

Learn to add, remove, toggle, and check CSS classes on DOM elements using classList. The cleanest way to change how elements look from JavaScript.

4 min read

classList is the cleanest way to manage CSS classes on a DOM element from JavaScript. Instead of manipulating the className string and worrying about accidentally removing other classes, classList gives you simple methods: add, remove, toggle, contains, and replace. Each one targets a specific class without touching the rest.

javascriptjavascript
const panel = document.querySelector("#panel");
panel.classList.add("open");

One line adds the class "open" to the panel. If the panel already had other classes, they stay untouched. If "open" was already there, nothing changes and no error is thrown.

The Five Methods

classList exposes five methods. Four of them are used daily. The fifth, replace, is newer but useful in specific cases.

MethodWhat it doesReturns
add("name")Adds the class if not presentundefined
remove("name")Removes the class if presentundefined
toggle("name")Adds if absent, removes if presenttrue/false
contains("name")Checks if the class is presenttrue/false
replace("old", "new")Swaps one class for anothertrue/false

add and remove: Apply and Clear Styles

The add and remove methods are the most common. They let you apply and clear visual states without touching the element's other classes.

javascriptjavascript
const box = document.querySelector("#box");
box.classList.add("highlighted");
box.classList.add("bordered");

The box now has both "highlighted" and "bordered" classes in addition to any classes it already had. To remove one:

javascriptjavascript
box.classList.remove("highlighted");

Now only "bordered" remains plus the original classes. You can add or remove multiple classes in one call by passing extra arguments:

javascriptjavascript
box.classList.add("large", "rounded", "shadow");
box.classList.remove("large", "shadow");

toggle: The On/Off Switch

The toggle method is built for binary states: open or closed, visible or hidden, dark or light. Call it with a class name and it flips that class on or off.

javascriptjavascript
const menu = document.querySelector("#dropdown");
menu.classList.toggle("open");

If "open" is absent, toggle adds it. If "open" is already present, toggle removes it. This single method powers every hamburger menu, collapsible panel, and show/hide button on the web.

The return value tells you which direction the toggle went:

javascriptjavascript
const isNowOpen = menu.classList.toggle("open");
 
if (isNowOpen) {
  console.log("Menu is now visible");
} else {
  console.log("Menu is now hidden");
}

contains: Check Before Acting

Use contains to check if a class is present. It returns true or false without changing anything, which is useful whenever you need to know the current state before updating the UI.

javascriptjavascript
const themeButton = document.querySelector("#theme-toggle");
const isDark = document.body.classList.contains("dark");
 
themeButton.textContent = isDark ? "Switch to Light" : "Switch to Dark";

The button label is set based on whether the "dark" class is currently on the body element, without changing that class at all. Combining contains with toggle lets you update button text right after a state change too, since toggle's return value tells you the new state directly.

replace: Swap One Class for Another

The replace method swaps one class for another in a single step. It is newer than the other methods but supported in all modern browsers.

javascriptjavascript
element.classList.replace("old-theme", "new-theme");

If "old-theme" is not present, replace returns false and nothing changes. If it is present, the swap happens and replace returns true.

Why classList Over className

The older className property replaces the entire class attribute at once:

javascriptjavascript
element.className = "open";  // Removes all other classes

This wipes out every other class on the element. classList methods target individual classes, leaving everything else alone. This is why classList is the standard approach in modern code.

For the broader topic of changing element appearance, see changing CSS styles with JavaScript. For selecting the elements you want to modify, see how to select DOM elements.

Rune AI

Rune AI

Key Insights

  • classList.add and classList.remove change specific classes without touching others.
  • classList.toggle adds a class if absent and removes it if present.
  • classList.contains lets you check if a class is already applied.
  • All classList methods work without affecting unrelated classes on the element.
  • Pair classList with CSS stylesheets for cleaner separation of style and logic.
RunePowered by Rune AI

Frequently Asked Questions

What is the difference between classList and className?

className replaces the entire class attribute with a string, which means you must manage all classes as one concatenated value. classList gives you individual methods to add, remove, and toggle specific classes without affecting others on the same element.

Does classList.toggle return a value?

Yes. classList.toggle returns true if the class was added, and false if it was removed. This is useful for updating UI state like button text or icons based on whether a class is now present.

Can I use classList on SVG elements?

Yes, classList works on both HTML and SVG elements in all modern browsers. Older versions of Internet Explorer had limited SVG classList support, but all current browsers handle it correctly.

Conclusion

classList is the right tool for changing element appearance from JavaScript. Use add and remove to apply and clear styles. Use toggle for on/off states like menus, modals, and theme switches. Use contains to check current state before acting. By keeping your visual rules in CSS files and using classList only to switch them on and off, your styling stays organized and easy to maintain.