We can use jQuery to remove a class from an element simply by using the removeClass() method.
$("#div").removeClass("existing-class");
Let’s say I have the following HTML:
This is a paragraph.
If we want to remove the class “existing-class” from #div, we can use the jQuery removeClass() method to do this with the following JavaScript code.
$("#div").removeClass("existing-class");
The resulting HTML would be as follows:
This is a paragraph.
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery("#div").removeClass("remove-class");
Removing a Class from HTML Element Using jQuery With a Click
We can remove a class from a HTML element using jQuery very easily by combining the removeClass() 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 the “p” class from the second paragraph. By removing this class, the text will not be underlined anymore.
This is the paragraph we will remove the class from.
Remove Class
We can utilize both the jQuery click() method and jQuery removeClass() method to remove the class from the paragraph and remove the underline.
Below is the JavaScript code which will allow the user to be able to remove the class from the paragraph:
$("#click-me").click(function(){
$("#paragraph").removeClass("p");
});
The final code and output for this example of how to remove a class using jQuery and JavaScript is below:
Code Output:
This is the paragraph we will remove the class from.
Full Code:
This is the paragraph we will remove the class from.
Remove Class
Using jQuery to Remove Multiple Classes from a HTML Element
We can use the jQuery removeClass() method to remove multiple classes to an HTML element very easily.
The key to removing multiple classes to our HTML elements is putting a space in between the classes we want to remove in the call to removeClass()
For example, if we want to remove classes “class-1” and “class-2” from a specific div, we can do so with the following JavaScript code:
$("#div").removeClass("class-1 class-2");
Let’s say we have the following HTML:
This is the paragraph we will remove the classes from.
Remove Classes
If we want to remove the classes “class-1” and “class-2” from the paragragh after a click, we just need to do the following in our JavaScript code:
$("#click-me-1").click(function(){
$(".class-1").removeClass("class-1 class-2");
});
The result will be that the paragraph will not have a background color of yellow and will not have all of its text underlined.
The final code and output for this example of how to remove a class using jQuery and JavaScript is below:
Code Output:
This is the paragraph we will remove the classes from.
Full Code:
This is the paragraph we will remove the classes from.
Remove Classes
Using jQuery to Remove All Classes from a HTML Element
We can use the jQuery removeClass() method to remove all classes from an HTML element very easily.
Removing all classes from an HTML element is done by leaving the call removeClass() empty.
For example, if we want to remove all classes from a specific paragraph, we can do so with the following JavaScript code:
$("#div").removeClass();
Let’s say we have the following HTML:
This is the paragraph we will remove ALL classes from.
Remove All Classes
If we want to remove the classes “class-3”, “class-4”, and “class-5” from this div after a click, we just need to do the following in our JavaScript code:
$("#click-me-2").click(function(){
$(".class-3").removeClass();
});
The result will be that the paragraph will not have a background color of yellow, will not be bold, and will not have all of its text underlined.
The final code and output for this example of how to remove all classes using jQuery and JavaScript is below:
Code Output:
This is the paragraph we will remove ALL classes from.
Full Code:
This is the paragraph we will remove ALL classes from.
Remove All Classes
Hopefully this article has been useful for you to understand how to use jQuery to remove a class.