To remove a class from an HTML element using Javascript, the simplest way is to get the classList of the element and then use the remove() method.

document.getElementById("div1").classList.remove("old-class");

Let’s say I have the following HTML:

This is a paragraph.

If we want to remove the class “existing-class” from #div1, we can use Javascript to get the element, get the element’s classList, and then remove the class from its classList.

function clickFunction() {
  var divToRemoveClass = document.getElementById("div1");
  divToRemoveClass.classList.remove("existing-class");
}

The resulting HTML would be as follows:

This is a paragraph.

Removing a Class of an HTML Element Using Javascript With a Click

We can remove a class from an HTML element using Javascript very easily by combining the remove() method with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to remove an existing class from the paragraph. The new class will remove the underline from the paragraph content.


This is the paragraph we will remove a class from.

Remove class

We can utilize both the onclick event and the remove() method to remove a class from the paragraph and remove the underline from the paragraph.

Below is the Javascript code which will allow the user to be able to remove the class from the paragraph:

function clickFunction() {
     var currP = document.getElementById("p1");
     currP.classList.remove("underline-class");
}

The final code and output for this example of how to remove a class using Javascript is below:

Code Output:

This is the paragraph we will remove a class from.

Remove class

Full Code:


This is the paragraph we will remove a class from.

Remove class
<script> function clickFunction() { var currP = document.getElementById("p1"); currP.classList.underline-class("underline-class"); } </script>

Hopefully this article has been useful for you to understand how to remove a class from an HTML element using Javascript.

Categorized in:

JavaScript,

Last Update: March 21, 2024