Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove all CSS classes using jQuery/JavaScript?

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.

like image 910
Ali Avatar asked Sep 15 '09 03:09

Ali


People also ask

How do you remove CSS class element using jQuery?

To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element.

How do I remove all classes from an element?

To remove all classes from an element, use the removeAttribute() method, e.g. box. removeAttribute('class') . The method removes will remove the class attribute from the element, effectively removing all of the element's classes.

Can you remove a class with jQuery?

As of jQuery 1.4, the . removeClass() method allows us to indicate the class to be removed by passing in a function.

How add or remove a class in jQuery?

addClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements.


2 Answers

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

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


You can also use (but it 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 = ''; 
like image 159
jimyi Avatar answered Oct 03 '22 03:10

jimyi


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

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

will do it on its own...

like image 35
da5id Avatar answered Oct 03 '22 03:10

da5id