views:

3135

answers:

5

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.

+2  A: 

Of course.

$('#item')[0].className = '';
// or
document.getElementById('item').className = '';
kangax
Why was this reduced?
David Andres
+2  A: 

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();
Isaac Waller
+17  A: 
$("#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 = '';
jimyi
+3  A: 

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

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

will do it on its own...

da5id
Don't think so: http://docs.jquery.com/Attributes/removeClass
Isaac Waller
yeah: "Removes all or the specified class(es) from the set of matched elements."
da5id
@Isaac Waller: it *does* work.
voyager
A: 

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

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

prashant