Javatpoint Logo
Javatpoint Logo

How to remove class in JavaScript


The Document Object Model (DOM) is an essential idea in HTML and XML. HTML frequently uses DOM manipulation for a number of reasons. HTML elements can be added or removed using DOM manipulation. JavaScript allows us to add or remove properties, classes, and IDs from HTML. We can also utilize other JavaScript tools like JQuery to manipulate the DOM. In this tutorial, we will discover how to remove a class from JavaScript.

Need of this concept:

There are several reasons why removing classes from HTML may be necessary. We encountered one instance when we used HTML, CSS, and JavaScript to create a slider. We created the main class while developing our slider, and then we played around with it by adding and removing it from images. As a result, our slider could change photos. You can read the article here if you are not familiar with this.

It is simpler for us to alter HTML Elements due to JavaScript, and adding or removing items and characteristics is simple. Since Jquery is a JavaScript library, we can also utilize it to remove the class. Jquery and JavaScript both have a long variety of built-in methods that can be used to remove classes. Each one will be examined in turn so that we may comprehend how it operates.

A to-do list has been created using JavaScript, CSS, and HTML. The user enters the task they must carry out. All of these tasks are active by default and must be completed. Now that a task has been completed, the user can cross it off their list of things to accomplish.

The to-do list items initially contain the class=active prefix. It informs the user that the task is still open and needs to be completed. After finishing the task, the user must remove class=active from the action item's to-do list. The user will be informed that the task has been finished in this way. Using JavaScript or Jquery, you can remove the class from HTML.

You may quickly remove a class using JavaScript or Jquery. Let's examine and comprehend how to remove a class from JavaScript.

How Can a Class Be Removed in JavaScript?

You can remove a class using a variety of built-in methods that are offered by both JavaScript and JQuery. Let's examine each of these approaches and learn how they function.

Remove Method:

JavaScript has a straightforward function called remove that you can use to remove classes. Specifying the element from which you wish to remove the class will do. The remove function of the chosen element's classList function can be called after the provided element. The classList function lists every class connected to the given element. You can choose the class that you want to remove from the class list and provide it to the remove method. The remove method also accepts multiple arguments. You can specify all the classes that you want to remove from the element. It makes it simpler to work with when developing complex web applications that need handling several classes for the same element.


toggleClass Method:

The delete method in JavaScript is comparable to the toggleClass method. The primary distinction is that it just temporarily removes some aspects of the class rather than totally eliminating it. When you want to toggle a class, you utilize the toggleClass function.

You can add or delete the same class from the element using the toggleClass function. For instance, let's say that you wish to remove a class whenever a button is pressed. Although, you want to add the same class that was deleted when the button is clicked again. In this situation, you can employ the toggleClass function. The toggleClass method can only be used with JQuery.

The toggleClass function is used on an element from which you want to toggle class, much like the remove method.


removeClass Method:

Use the removeClass function in jQuery to remove the class rather than toggle it. The JQuery is completely eliminated using the removeClass function. The syntax of removeClass and toggleClass are identical, but their functionalities are entirely different. Without using the addClass method in JQuery, a class that has already been removed via the removeClass method cannot be added again. It is eliminated from the element forever.


Let's practice removing classes in JavaScript using some real-world examples.

Example to remove class in JavaScript:

Now, we will see examples of class removal in JavaScript and JQuery. We will also gain knowledge of all three remove class methods in JavaScript and JQuery through practical application.

In the real world, we typically use particular types of event listeners when developing any feature that necessitates the removal of a class. We start the procedure that removes the class from the element when the user takes a specific action, such as pushing a key or clicking a button.

Some examples of removing classes in JavaScript:

Example of using removes method:

In this example, we'll remove a class from an HTML Element using JavaScript's remove method.


In the example above, we remove the class main from the div element. We chose the element using the distinct element id mainDiv. After that, we included the event listener onclick, which launches the removeClass method when the user clicks the button. The function makes it simple to delete the element's class main.

Example of using toggleClass Method:

In this demonstration, we'll use JQuery's toggleClass method to remove a class from an HTML element.


In the example above, we removed the class main from the div element. We chose the element using the distinct element id mainDiv. After that, we add the event listener onclick, and when the user clicks the button, it activates the toggleClass method. The function makes it simple to switch the element's class main. This class can be removed and added again to the mainDiv by the user. This approach does not completely delete the class from the mainDiv function like the remove class method does. When the button is clicked twice, the class is added back after being removed the first time.

If you plan to use this method, make sure to include the JQuery script in the head tag or at the end of the body tag. Otherwise, the function won't be recognized by the browser, and its impact won't be rendered.

Example of using the removeClass Method:

In this example, we'll be removing a class from an HTML Element using JQuery's removeClass method.


In the example above, we delete the class main from the div element. We chose the element using the distinct element id mainDiv. When the user hits the button, the event listener onclick that activates the removeClassJQuery function is added. The function makes it simple to delete the element's class main.


  1. You can remove classes with DOM Manipulation in JavaScript and associated libraries, such as JQuery.
  2. Depending on how you want to remove the class and which library you're using, you can employ a number of different techniques.
  3. The remove method in JavaScript allows you to remove the class attribute from any HTML element permanently.
  4. The removeClass method provided by JQuery permanently removes a class from any HTML Element.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA