.removeClass()

Remove a single class or multiple classes from each element in the set of matched elements. Remove all classes from each matched element.

.removeClass(className)🡢 jQuery

className StringOne or more space-separated classes to be removed from the class attribute of each matched element.

.removeClass(classNames)🡢 jQuery

classNames ArrayAn array of classes to be removed from the class attribute of each matched element.

.removeClass(function(indexInteger, classNameString))🡢 String

function(indexInteger, classNameString) FunctionA function returning one or more space-separated class names to be removed. Receives the index position of the element in the set and the old class value as arguments.

.removeClass(function(indexInteger, classNameString))🡢 String, Array

function(indexInteger, classNameString) FunctionA function returning one or more space-separated class names or an array of class names to be removed. Receives the index position of the element in the set and the old class value as arguments.

Before jQuery version 1.12/2.2, the .removeClass() method manipulated the className property of the selected elements, not the class attribute. Once the property was changed, it was the browser that updated the attribute accordingly. This means that when the class attribute was updated and the last class name was removed, the browser might have set the attribute's value to an empty string instead of removing the attribute completely. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).

As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class attribute is used instead. So, .removeClass() can be used on XML or SVG documents.

More than one class may be removed at a time, separated by a space, from the set of matched elements, like so:

$("p").removeClass("myClass yourClass");

This method is often used with .addClass() to switch elements' classes from one to another, like so:

$("p").removeClass("myClass noClass").addClass("yourClass");

Here, the myClass and noClass classes are removed from all paragraphs, while yourClass is added.

To replace all existing classes with another class, we can use .attr( "class", "newClass" ) instead.

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

$("li")
  .last()
  .removeClass(function () {
    return $(this).prev().attr("class");
  });

This example removes the class name of the penultimate <li> from the last <li>.

Remove the class 'blue' from the matched elements.

JS
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
CSS
p {
  margin: 4px;
  font-size: 16px;
  font-weight: bolder;
}
.blue {
  color: blue;
}
.under {
  text-decoration: underline;
}
.highlight {
  background: yellow;
}
HTML
$("p").even().removeClass("blue");
DEMO

Remove the class 'blue' and 'under' from the matched elements.

JS
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
CSS
p {
  margin: 4px;
  font-size: 16px;
  font-weight: bolder;
}
.blue {
  color: blue;
}
.under {
  text-decoration: underline;
}
.highlight {
  background: yellow;
}
HTML
$("p").odd().removeClass("blue under");
DEMO

Remove the class 'blue' and 'under' from the matched elements (3.3+ syntax).

JS
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
CSS
p {
  margin: 4px;
  font-size: 16px;
  font-weight: bolder;
}
.blue {
  color: blue;
}
.under {
  text-decoration: underline;
}
.highlight {
  background: yellow;
}
HTML
$("p").odd().removeClass(["blue", "under"]);
DEMO

.removeClass()🡢 jQuery

Before jQuery version 1.12/2.2, the .removeClass() method manipulated the className property of the selected elements, not the class attribute. Once the property was changed, it was the browser that updated the attribute accordingly. This means that when the class attribute was updated and the last class name was removed, the browser might have set the attribute's value to an empty string instead of removing the attribute completely. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).

As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class attribute is used instead. So, .removeClass() can be used on XML or SVG documents.

Remove all the classes from the matched elements.

JS
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
CSS
p {
  margin: 4px;
  font-size: 16px;
  font-weight: bolder;
}
.blue {
  color: blue;
}
.under {
  text-decoration: underline;
}
.highlight {
  background: yellow;
}
HTML
$("p").eq(1).removeClass();
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)