Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery addClass() method

jQuery addClass() method

jQuery HTML/CSS methods jQuery HTML/CSS methods

Example

Add a class name to the first

element:

$

(

"

button

"

)

.

click

(

function

(

)

{

$

(

"

p:first

"

)

.

addClass

(

"

intro

"

)

;

}

)

;

Try it »


Definition and Usage

The addClass() method adds one or more class names to the selected elements.

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

**Tip:** To add multiple classes, separate the class names with spaces.


Syntax

$(

selector

).addClass(

classname

,function(

index,oldclass

))

Parameters Description
classname Required. Specifies one or more class names to add.
function(index,currentclass) Optional. Specifies a function that returns one or more class names to add.
  • index - Returns the index position of the element in the set.
  • currentclass - Returns the current class name of the selected element.
  • |

    More examples

    Adding two classes to an element
    How to add two class names to a specified element.

    Adding classes using a function
    How to use a function to add classes to the selected elements.

    Changing an element's class name
    How to use addClass() and removeClass() to remove one class name and add a new one.


    jQuery HTML/CSS methods jQuery HTML/CSS methods