Home » Jquery » How to remove all CSS classes using jQuery?

How to remove all CSS classes using jQuery?

Posted by: admin November 2, 2017 Leave a comment

Questions:

Instead of individually calling $("#item").removeClass() for every single class an element might have, is there a single function which can be called which removes all CSS classes from the given element?

Both jQuery and raw JavaScript will work.

Answers:
$("#item").removeClass();

Calling removeClass with no parameters will remove all of the item’s classes.


You can also use (but is not necessarily recommended, the correct way is the one above):

$("#item").removeAttr('class');
$("#item").attr('class', '');
$('#item')[0].className = '';

If you didn’t have jQuery, then this would be pretty much your only option:

document.getElementById('item').className = '';

Questions:
Answers:

Hang on, doesn’t removeClass() default to removing all classes if nothing specific is specified? So

$("#item").removeClass();

will do it on its own…

Questions:
Answers:

Just set the className attribute of the real DOM element to '' (nothing).

$('#item')[0].className = ''; // the real DOM element is at [0]

Edit: Other people have said that just calling removeClass works – I tested this with the Google JQuery Playground: http://savedbythegoog.appspot.com/?id=ag5zYXZlZGJ5dGhlZ29vZ3ISCxIJU2F2ZWRDb2RlGIS61gEM … and it works. So you can also do it this way:

$("#item").removeClass();

Questions:
Answers:

Of course.

$('#item')[0].className = '';
// or
document.getElementById('item').className = '';

Questions:
Answers:

Heh, came searching for a similar answer.
Then it hit me.

Remove Specific Classes

$('.class').removeClass('class');

Say if element has class=”class another-class”

Questions:
Answers:

The shortest method

$('#item').removeAttr('class').attr('class', '');

Questions:
Answers:
$('#elm').removeAttr('class');

no longer class attr wil be present in “elm”.

Questions:
Answers:

try with removeClass

For instance:

var nameClass=document.getElementsByClassName("clase1");
console.log("after", nameClass[0]);
$(".clase1").removeClass();
var nameClass=document.getElementsByClassName("clase1");
console.log("before", nameClass[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="clase1">I am Div with class="clase1"</div>

Questions:
Answers:

I like using native js do this, belive it or not!

1.

// remove all items all class  
const items = document.querySelectorAll('item');
for (let i = 0; i < items.length; i++) {
    items[i].className = '';
}

2.

// only remove all class of first item
const item1 = document.querySelector('item');
item1.className = '';

jQuery ways

  1. $("#item").removeClass();

  2. $("#item").removeClass("class1 ... classn");

Questions:
Answers:

You can just try

$(document).ready(function() {
   $('body').find('#item').removeClass();
});

If you have to access to that element without class name, for example you have to add a new class name, you can do that:

$(document).ready(function() {
   $('body').find('#item').removeClass().addClass('class-name');
});

I use that function in my projet to remove and add class in a html builder. Good luck.

Questions:
Answers:

Since not all versions of jQuery are created equal, you may run into the same issue I did which means calling $(“#item”).removeClass(); does not actually remove the class. (Probably a bug)

A more reliable method is to simply use raw JavaScript and remove the class attribute altogether.

document.getElementById("item").removeAttribute("class");

Questions:
Answers:

I had similar issue. In my case on disabled elements was applied that aspNetDisabled class and all disabled controls had wrong colors. So, I used jquery to remove this class on every element/control I wont and everything works and looks great now.

This is my code for removing aspNetDisabled class:

$(document).ready(function () {

    $("span").removeClass("aspNetDisabled");
    $("select").removeClass("aspNetDisabled");
    $("input").removeClass("aspNetDisabled");

});