Remove all CSS classes from an HTML element with JavaScript/jQuery
This post will discuss how to remove all CSS classes from an element using JavaScript and jQuery.
1. Using jQuery’s .removeAttr() method
With jQuery, you can use the .removeAttr() method to remove the class attribute from an element. It is equivalent to the JavaScript’s removeAttribute() method.
JS
|
1 2 3 |
$(document).ready(function() { $("#container").removeAttr("class"); }); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="main"></div> |
However, this won’t remove any inline styling applied to the element. We can handle this removing the style attribute from the element.
JS
|
1 2 3 |
$(document).ready(function() { $("#container").removeAttr("style"); }); |
HTML
|
1 |
<div id="container" style="width: 500px; height: 300px; border: 1px solid blue;"></div> |
2. Using jQuery’s .removeClass() method
You can also use the no-arg .removeClass() method for removing all class from the element. Note, this won’t remove any inline styling applied to the element.
JS
|
1 2 3 |
$(document).ready(function() { $("#container").removeClass(); }); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="main"></div> |
3. Using jQuery’s .attr() method
Setting the class attribute to empty will remove all classes from the element but also leaves an empty class attribute on the DOM.
JS
|
1 2 3 |
$(document).ready(function() { $("#container").attr("class", ""); }); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="main"></div> |
To remove the inline styling applied to the element, you can set the style attribute to empty:
JS
|
1 2 3 |
$(document).ready(function() { $("#container").attr("style", ""); }); |
HTML
|
1 |
<div id="container" style="width: 500px; height: 300px; border: 1px solid blue;"></div> |
4. Using JavaScript’s removeAttribute() method
In plain JavaScript, you can use the removeAttribute() method to remove the class attribute from an element. To remove all the inline styles as well, you can remove the style attribute as well.
JS
|
1 |
document.getElementById('container').removeAttribute('class'); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="main"></div> |
5. Using JavaScript’s className property
Alternatively, you can set the className attribute to empty, which would set the value of the class attribute of the specified element to null.
This won’t remove the inline styles from the element, but you can remove them by setting the style attribute to empty:
JS
|
1 |
document.getElementById('container').className = ''; |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="main"></div> |
That’s all about removing all CSS classes from an element using JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)