How to Remove a Class from an Element in JavaScript

In JavaScript, removing a CSS class from an element is essential for dynamically updating the appearance of elements. Whether you're toggling between themes, changing styles based on user interaction, or resetting specific styles, removing a class is a common task. This post will show how to remove a class from an element using different methods.

Table of Contents

  1. Using classList.remove()
  2. Removing Multiple Classes
  3. Checking if a Class Exists Before Removing
  4. Using className Property (Legacy Method)
  5. Conclusion

1. Using classList.remove()

The classList.remove() method is the simplest and most effective way to remove a class from an element. It ensures the class is removed if it exists, without throwing an error if the class isn’t found.

Example:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .highlight {
      color: white;
      background-color: red;
      padding: 10px;
    }
  </style>
  <title>Remove Class Example</title>
</head>
<body>
  <h1 id="heading" class="highlight">Hello, World!</h1>
  <button onclick="removeHighlight()">Remove Highlight</button>
  <script src="script.js"></script>
</body>
</html>

JavaScript (script.js):

function removeHighlight() {
  const heading = document.getElementById("heading");
  heading.classList.remove("highlight");
}

Explanation:

  • document.getElementById("heading") selects the <h1> element.
  • heading.classList.remove("highlight") removes the "highlight" class from the element.

Output:

When you click the "Remove Highlight" button, the .highlight class will be removed, and the styling associated with that class will no longer apply.

2. Removing Multiple Classes

You can remove multiple classes from an element by passing multiple class names to classList.remove().

Example:

function removeMultipleClasses() {
  const heading = document.getElementById("heading");
  heading.classList.remove("highlight", "bold-text", "extra-padding");
}

Explanation:

  • You can remove multiple classes at once by passing them as separate arguments to classList.remove().

Example:

<h1 id="heading" class="highlight bold-text extra-padding">Hello, World!</h1>

After calling the function, all three classes (highlight, bold-text, and extra-padding) will be removed.

3. Checking if a Class Exists Before Removing

To avoid unnecessary operations, you can first check whether the element has the class using classList.contains() before attempting to remove it.

Example:

function removeHighlightIfExists() {
  const heading = document.getElementById("heading");
  
  if (heading.classList.contains("highlight")) {
    heading.classList.remove("highlight");
  }
}

Explanation:

  • classList.contains("highlight") checks if the element has the "highlight" class. If it does, the class is removed. This method can be useful in complex scenarios where you want to ensure the class is present before attempting to remove it.

4. Using className Property (Legacy Method)

Before classList was widely supported, developers used the className property to remove classes. This method requires manipulating the entire class string, which can be error-prone, but may still be useful for compatibility with older browsers.

Example:

function removeHighlightLegacy() {
  const heading = document.getElementById("heading");
  heading.className = heading.className.replace("highlight", "").trim();
}

Explanation:

  • The className property holds the entire list of classes as a string. By using replace(), you remove the specific class ("highlight") from the class list.
  • Caution: This method is less efficient and can lead to issues if not handled carefully, as it involves manual string manipulation.

Example with Multiple Classes:

<h1 id="heading" class="highlight bold-text extra-padding">Hello, World!</h1>
function removeHighlightLegacy() {
  const heading = document.getElementById("heading");
  heading.className = heading.className.replace("highlight", "").trim();
}

The .highlight class is removed from the string, leaving "bold-text extra-padding" intact.

5. Conclusion

Removing a class from an element in JavaScript is straightforward with the classList.remove() method, which is the recommended approach for modern browsers. This method ensures that the class is removed efficiently and safely without needing manual string manipulation. However, for legacy browsers, you may need to work with the className property, though it's less reliable.

Key Points:

  • classList.remove(): The modern and efficient way to remove one or more classes.
  • Multiple Classes: You can remove multiple classes by passing them as arguments to classList.remove().
  • classList.contains(): You can check if a class exists before removing it, ensuring the operation is valid.
  • className: A legacy method that involves manual string manipulation and should be avoided if possible.

Comments