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:
<div id="div" class="existing-class">
<p class="p">This is a paragraph.</p>
</div>
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:
<div id="div">
<p class="p">This is a paragraph.</p>
</div>
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.
<style>.p { text-decoration:underline; } </style>
<div id="div1">
<p id="paragraph" class="p">This is the paragraph we will remove the class from.</p>
<div id="click-me">Remove Class</div>
</div>
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:
<style>.p { text-decoration:underline; } </style>
<div id="div1">
<p id="paragraph" class="p">This is the paragraph we will remove the class from.</p>
<div id="click-me">Remove Class</div>
</div>
<script>
$("#click-me").click(function(){
$("#paragraph").removeClass("p");
});
</script>
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:
<style>.class-1 { text-decoration:underline; } .class-2 { background-color: yellow; }</style>
<div id="div1">
<p class="class-1 class-2">This is the paragraph we will remove the classes from.</p>
<div id="click-me-1" class="click-me">Remove Classes</div>
</div>
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:
<style>.class-1 { text-decoration:underline; } .class-2 { background-color: yellow; }</style>
<div id="div1">
<p class="class-1 class-2">This is the paragraph we will remove the classes from.</p>
<div id="click-me-1" class="click-me">Remove Classes</div>
</div>
<script>
$("#click-me-1").click(function(){
$(".class-1").removeClass("class-1 class-2");
});
</script>
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:
<style>.class-3 { text-decoration:underline; } .class-4 { background-color: yellow; } .class-5 { font-weight:700; }</style>
<div id="div2">
<p class="class-3 class-4 class-5">This is the paragraph we will remove ALL classes from.</p>
<div id="click-me-2" class="click-me">Remove All Classes</div>
</div>
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:
<style>.class-3 { text-decoration:underline; } .class-4 { background-color: yellow; } .class-5 { font-weight:700; }</style>
<div id="div2">
<p class="class-3 class-4 class-5">This is the paragraph we will remove ALL classes from.</p>
<div id="click-me-2" class="click-me">Remove All Classes</div>
</div>
<script>
$("#click-me-2").click(function(){
$(".class-3").removeClass();
});
</script>
Hopefully this article has been useful for you to understand how to use jQuery to remove a class.
Leave a Reply