How to Remove CSS Class JavaScript

Harshit Jindal Mar 13, 2025 JavaScript
  1. Understanding the Basics of CSS Classes
  2. Method 1: Using the classList.remove() Method
  3. Method 2: Using the setAttribute() Method
  4. Method 3: Using jQuery for Class Manipulation
  5. Conclusion
  6. FAQ
How to Remove CSS Class JavaScript

Removing a CSS class from an element using JavaScript is a fundamental skill for web developers. Whether you’re looking to enhance user interaction, toggle styles dynamically, or manage class states, understanding how to manipulate CSS classes is essential. In this tutorial, we’ll explore the various methods to achieve this, ensuring you have a solid grasp of the concepts and code involved.

In the world of web development, JavaScript is a powerful tool that allows you to interact with the Document Object Model (DOM). By mastering how to remove CSS classes, you can create more engaging and responsive web applications. This guide will take you through step-by-step instructions, complete with code examples, to help you remove CSS classes effectively and efficiently.

Understanding the Basics of CSS Classes

Before diving into the methods for removing CSS classes, let’s first clarify what CSS classes are. CSS classes are used to apply specific styles to elements in HTML. By assigning a class to an element, you can control its appearance and behavior through CSS. However, there are times when you may want to remove a class to change the element’s styling or functionality dynamically.

When you remove a CSS class using JavaScript, you can manipulate the visual aspects of your webpage without needing to reload or refresh it. This capability is crucial for creating a seamless user experience. Now, let’s explore different methods to remove CSS classes from elements using JavaScript.

Method 1: Using the classList.remove() Method

One of the most straightforward and efficient ways to remove a CSS class from an element is by using the classList.remove() method. This method allows you to remove one or more classes from an element’s class list. Here’s how you can do it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Remove CSS Class Example</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div id="myElement" class="highlight">Hello World!</div>
    <button id="removeClassBtn">Remove Highlight Class</button>

    <script>
        document.getElementById('removeClassBtn').addEventListener('click', function() {
            document.getElementById('myElement').classList.remove('highlight');
        });
    </script>
</body>
</html>

In this example, we have a div with the class highlight, which applies a yellow background. When the button is clicked, the JavaScript event listener triggers the classList.remove() method, effectively removing the highlight class from the div. This method is clean and concise, making it a preferred choice for many developers.

Method 2: Using the setAttribute() Method

Another method to remove a CSS class is by using the setAttribute() method. While this approach is less common than classList.remove(), it allows for more flexibility, especially if you need to manipulate multiple classes at once. Here’s how it works:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Remove CSS Class Example</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div id="myElement" class="highlight another-class">Hello World!</div>
    <button id="removeClassBtn">Remove Highlight Class</button>

    <script>
        document.getElementById('removeClassBtn').addEventListener('click', function() {
            var element = document.getElementById('myElement');
            var classes = element.className.split(' ');
            var newClasses = classes.filter(function(className) {
                return className !== 'highlight';
            });
            element.setAttribute('class', newClasses.join(' '));
        });
    </script>
</body>
</html>

In this code, we first retrieve the current class names as a string and split them into an array. We then filter out the class we want to remove, in this case, highlight. Finally, we use setAttribute() to set the new class list back onto the element. This method is particularly useful when you need to remove a class while preserving others, as it gives you complete control over the class list.

Method 3: Using jQuery for Class Manipulation

If you are working with jQuery, removing a CSS class becomes even simpler. jQuery provides a built-in method called removeClass() that allows you to remove one or more classes from selected elements. Here’s a quick example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Remove CSS Class Example</title>
    <style>
        .highlight {
            background-color: yellow;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div id="myElement" class="highlight">Hello World!</div>
    <button id="removeClassBtn">Remove Highlight Class</button>

    <script>
        $('#removeClassBtn').click(function() {
            $('#myElement').removeClass('highlight');
        });
    </script>
</body>
</html>

Using jQuery simplifies the syntax and makes it easier to work with DOM elements. In this example, when the button is clicked, the removeClass() method is called on the #myElement, removing the highlight class with minimal code. This method is particularly beneficial for developers who are already using jQuery in their projects, as it integrates seamlessly with other jQuery functionalities.

Conclusion

In this tutorial, we explored various methods for removing CSS classes using JavaScript, including the classList.remove() method, the setAttribute() method, and jQuery’s removeClass() method. Each approach has its own advantages, depending on your specific needs and the complexity of your project. Mastering these techniques will enhance your ability to create dynamic and responsive web applications.

As you continue your journey in web development, remember that manipulating CSS classes is just one of the many powerful capabilities JavaScript offers. Keep experimenting, and you’ll discover even more ways to enhance user experiences on your websites.

FAQ

  1. How do I remove multiple CSS classes at once using JavaScript?
    You can use the classList.remove() method and pass multiple class names as arguments. Alternatively, you can use the setAttribute() method to filter out unwanted classes.

  2. Can I remove a CSS class based on a condition?
    Yes, you can use conditional statements in your JavaScript code to determine whether to remove a class, allowing for dynamic class manipulation based on user interactions or other conditions.

  3. Is it necessary to use jQuery to remove CSS classes?
    No, while jQuery simplifies the process, you can achieve the same result using plain JavaScript. The choice between using jQuery or vanilla JavaScript depends on your project requirements and personal preference.

  4. What happens if I try to remove a class that doesn’t exist?
    If you attempt to remove a class that is not present on the element, JavaScript will not throw an error. The operation will simply have no effect, and the class list will remain unchanged.

  5. Can I animate the removal of a CSS class?
    Yes, you can use CSS transitions or animations to create a smooth visual effect when a class is removed. Combine JavaScript class manipulation with CSS for enhanced user experiences.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Harshit Jindal avatar Harshit Jindal avatar

Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.

LinkedIn