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

This method does not remove existing class attributes, it only adds one or more values to the class attribute.

Tip: To add more than one class, separate the class names with space.


Syntax

$(selector).addClass(class)

Parameter Description
class Required. Specifies one or more class names to be added


Add Class Using a Function

Using a function to add classes to selected elements.

Syntax

$(selector).addClass(function(index,oldclass))
Try it yourself »

Parameter Description
function(index,oldclass) Required. Specifies a function that returns one or more class names to be added.
  • index - Optional. Receives the index position of the selector
  • oldclass - Optional. Receives the old class value of the selector


Examples

Try it Yourself - Examples

Add two classes to an element
How to add two class names to a selected element.

Change the class of an element
How to use addClass() and removeClass() to remove one class, and add a new class.