jQuery Attribute Operation - addClass() Method

Example

Add a class to the first p element:

$("button").click(function(){
  $("p:first").addClass("intro");
});

Try it yourself

Definition and Usage

The addClass() method adds one or more classes to the selected element.

This method will not remove the existing class attribute, but only add one or more class attributes.

Tip:If you need to add multiple classes, please use space-separated class names.

Syntax

$.addClass(class)
Parameters Description
class Required. Specifies one or more class names.

Add a class using a function

Use a function to add a class to the selected element.

Syntax

$.addClass(function(index,oldclass))

Try it yourself

Parameters Description
function(index,oldclass)

Required. Specifies a function that returns one or more classes to be added.

  • index - Optional. The index position of the selector.
  • class - Optional. The old class name of the selector.

More examples

Add two classes to the element
How to add two classes to the selected element.
Change the class of an element
As any use of addClass() and removeClass() to remove class, and add a new class.