snippetjavascriptCritical
How can I remove all CSS classes using jQuery/JavaScript?
Viewed 0 times
howjqueryremoveusingcsscanalljavascriptclasses
Problem
Instead of individually calling
Both jQuery and raw JavaScript will work.
$("#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.
Solution
$("#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 = '';Code Snippets
$("#item").removeClass();$("#item").removeAttr('class');
$("#item").attr('class', '');
$('#item')[0].className = '';document.getElementById('item').className = '';Context
Stack Overflow Q#1424981, score: 1707
Revisions (0)
No revisions yet.