How to use removeClass() method in jQuery

What is the removeClass() method?

The removeClass() method is used to remove one, multiple, or all the class names from the class attributes of the matched elements.

In an HTML document, elements may have a class attribute with one or more class names for that element. This class attribute is used by CSS or JavaScript to perform certain tasks on the element.

For instance, elements with the same class name in the class attribute can be used to apply the same style on them using CSS.


Syntax

The removeClass() method has three signatures:

1. No argument

  • removeClass(): Call to the method
  • No argument: When no argument is passed, all the class names are removed from the selected element’s class attribute.
$().removeClass()

2. Space separated list as an argument

  • removeClass(): Call to the method
  • className: One or more space-separated class names to be removed from the class attribute of each matched element.
$().removeClass([className])

3. Function as an argument

  • removeClass(): Call to the method
  • function: A function returning one or more space-separated class names to be removed from the class attribute of each matched element. The function receives the index position of the element in the set and the old class value as arguments.
$().removeClass(function(){})

Examples

Here are some examples showing the use of removeClass() method signatures:

1. Removing all classes

Calling removeClass() method without any argument removes all the class names from the class attribute of the selected element.

  • HTML

2. Space separated list as an argument

Calling removeClass([classname]) method with space separated list as an argument removes all specified class names from the class attribute of the selected element.

  • HTML

3. Function as an argument

Calling removeClass(function) method with a function as an argument expects a space separated list of class names that are removed from the class attribute of the selected elements.

  • HTML
Copyright ©2024 Educative, Inc. All rights reserved