Friday, 11 August 2017

How to 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.


Answer




$("#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 = '';

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...