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

13

872

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.

Outwardly answered 15/9, 2009 at 3:34 Comment(0)
G
1693
$("#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 = '';
Gereld answered 15/9, 2009 at 3:37 Comment(5)
Shouldn't the attr() version be prop() now?Ibby
Calling removeClass() without parameters does not seem to work anymore in version 1.11.1Flake
@Flake Seems like a bug introduced in jQuery since their documentation explicitly states this: If a class name is included as a parameter, then only that class will be removed from the set of matched elements. If no class names are specified in the parameter, all classes will be removed. api.jquery.com/removeclassParodic
Yes, I also suspect it's a bug, and not by design. In the meantime, the following pure JavaScript works just fine. document.getElementById("item").removeAttribute("class");Flake
There is a problem with jQuery UI 1.9. If removeClass() didn't work use removeAttr('class')Voluminous
F
129

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

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

will do it on its own...

Fitted answered 15/9, 2009 at 3:38 Comment(2)
yeah: "Removes all or the specified class(es) from the set of matched elements."Fitted
Now it is documented properly: If no class names are specified in the parameter, all classes will be removed.Songstress
B
20

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

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

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();
Beaty answered 15/9, 2009 at 3:37 Comment(0)
A
15

Of course.

$('#item')[0].className = '';
// or
document.getElementById('item').className = '';
Aerostatics answered 15/9, 2009 at 3:36 Comment(0)
E
12

Remove specific classes:

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

Say if element has class="class another-class".

Emblaze answered 8/6, 2014 at 18:57 Comment(1)
The last sentence seems incomprehensible. Can you fix it?Typify
B
9

The shortest method

$('#item').removeAttr('class').attr('class', '');
Belfast answered 21/1, 2013 at 15:59 Comment(0)
S
8

You can just try:

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

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

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

I use that function in my project to remove and add classes in an HTML builder.

Sky answered 1/6, 2016 at 19:30 Comment(0)
F
7

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");
Flake answered 20/6, 2016 at 16:29 Comment(0)
L
6
$('#elm').removeAttr('class');

Attribute "class" will no longer be present in "elm".

Lourdeslourie answered 8/1, 2014 at 7:18 Comment(1)
this is what worked for me. removeClass(); did not work for me.Yourself
F
6

I like using native JavaScript to do this, believe it or not!

solution 1: className

  1. Remove all class of all items
const items = document.querySelectorAll('item');
for (let i = 0; i < items.length; i++) {
  items[i].className = '';
}
  1. Only remove all class of the first item
const item1 = document.querySelector('item');
item1.className = '';

solution 2: classList

// remove all class of all items
 const items = [...document.querySelectorAll('.item')];
 for (const item of items) {
   item.classList.value = '';
 }
 // remove all class of the first item
 const items = [...document.querySelectorAll('.item')];
 for (const [i, item] of items.entries()) {
   if(i === 0) {
     item.classList.value = '';
   }
 }
 // or
 const item = document.querySelector('.item');
 item.classList.value = '';

jQuery ways (not recommended)

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

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

refs

https://developer.mozilla.org/en-US/docs/Web/API/Element/className

https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Frasier answered 13/6, 2017 at 3:56 Comment(0)
C
2

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 a Div with class="clase1"</div>
Circlet answered 18/1, 2017 at 15:34 Comment(0)
T
2

Let's use this example. Maybe you want the user of your website to know a field is valid or it needs attention by changing the background color of the field. If the user hits reset then your code should only reset the fields that have data and not bother to loop through every other field on your page.

This jQuery filter will remove the class "highlightCriteria" only for the input or select fields that have this class.

$form.find('input,select').filter(function () {
    if((!!this.value) && (!!this.name)) {
        $("#"+this.id).removeClass("highlightCriteria");
    }
});
Techno answered 19/10, 2018 at 21:18 Comment(0)
F
-2

I had a 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 want 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");

});
Fellatio answered 14/5, 2014 at 14:57 Comment(1)
It is incomprehensible near "was applied that". Can you fix it? (But without "Edit:", "Update:", or similar - the question/answer should appear as if it was written today.)Typify

© 2022 - 2024 — McMap. All rights reserved.