jQuery API

.removeClass()

.removeClass( [className] ) Returns: jQuery

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

  • version added: 1.0.removeClass( [className] )

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

  • version added: 1.4.removeClass( function(index, class) )

    function(index, class)A 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.

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.

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

Examples:

Example: Remove the class 'blue' from the matched elements.

<!DOCTYPE html>
<html>
<head>
  <style>

  p { margin: 4px; font-size:16px; font-weight:bolder; }
  .blue { color:blue; }
  .under { text-decoration:underline; }
  .highlight { background:yellow; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <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>
<script>$("p:even").removeClass("blue");</script>

</body>
</html>

Demo:

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

<!DOCTYPE html>
<html>
<head>
  <style>
  p { margin: 4px; font-size:16px; font-weight:bolder; }
  .blue { color:blue; }
  .under { text-decoration:underline; }
  .highlight { background:yellow; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <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>
<script>$("p:odd").removeClass("blue under");</script>

</body>
</html>

Demo:

Example: Remove all the classes from the matched elements.

<!DOCTYPE html>
<html>
<head>
  <style>

  p { margin: 4px; font-size:16px; font-weight:bolder; }
  .blue { color:blue; }
  .under { text-decoration:underline; }
  .highlight { background:yellow; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <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>
<script>$("p:eq(1)").removeClass();</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .removeClass() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .removeClass()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Example for generic classes:

    $('.foo.bar20').addClass('baz20');
    $('.foo.bar40').addClass('baz40');

    // ... later

    // reset all

    $(.foo).removeClass (function (index, class) {
    var matches = class.match (/baz\d+/g) || [];
    return (matches.join (' '));
    });
  • Clarence L.
    do I need to check if the class exists with hasClass before using removeClass? I know it works without checking hasClass but is there a performance gain/loss?
  • Noorsulayman
    It seems to me that checking and then removing causes the performance loss. Just removing though, you should be fine. This is an assumption though
  • B. Stramke
    Is it better to use a selector like
    $("#FieldId.classname").removeClass('classname');
    or
    $("#FieldId").removeClass('classname');
    ?
  • tim_atom
    This is a good question for the forums, but the use of #id should always be isolated to gain maximum performance.
  • OgO
    Id is more speedly. This is normal. When you use element type, JavaScript scan all components in HTML code. This is a slow action on client side. If a browser use any of indexing mechanism for elements in page, that will be speedy too.