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, it will only add one or more class attributes.

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

Syntax

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

Use a function to add class

Use a function to add classes to the selected element.

Syntax

$.addClass(function(index,oldclass))

Try it yourself

Parameters Description
function(index,oldclass)

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

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

More Instance

Tsofi class zuwa elementi
Kuwa ce baiyowa tsofi elementi domin class.
Baiyowa elementi class
Daga baya ce dake addClass() da removeClass() domin class, kuma tsofi class tiya.