ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://api.jquery.com/addClass/ |
| Last Crawled | 2026-04-16 10:30:23 (2 days ago) |
| First Indexed | 2013-08-16 13:26:33 (12 years ago) |
| HTTP Status Code | 200 |
| Meta Title | .addClass() | jQuery API Documentation |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | .addClass( className )
Returns:
jQuery
Description:
Adds the specified class(es) to each element in the set of matched elements.
version added:
1.0
.addClass( className )
className
One or more space-separated classes to be added to the class attribute of each matched element.
version added:
3.3
.addClass( classNames )
classNames
An array of classes to be added to the class attribute of each matched element.
version added:
1.4
.addClass( function )
function
A function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function,
this
refers to the current element in the set.
version added:
3.3
.addClass( function )
function
A function returning one or more space-separated class names or an array of class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function,
this
refers to the current element in the set.
It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.
Before jQuery version 1.12/2.2, the
.addClass()
method manipulated the
className
property
of the selected elements, not the
class
attribute
. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).
As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the
class
attribute
is used instead. So,
.addClass()
can be used on XML or SVG documents.
More than one class may be added at a time, separated by a space, to the set of matched elements, like so:
1
$(
"p"
).addClass(
"myClass yourClass"
);
This method is often used with
.removeClass()
to switch elements' classes from one to another, like so:
1
$(
"p"
).removeClass(
"myClass noClass"
).addClass(
"yourClass"
);
Here, the
myClass
and
noClass
classes are removed from all paragraphs, while
yourClass
is added.
As of jQuery 1.4, the
.addClass()
method's argument can receive a function.
1
2
3
$(
"ul li"
).addClass(
function
(
index
)
{
return
"item-"
+ index;
});
Given an unordered list with two
<li>
elements, this example adds the class "item-0" to the first
<li>
and "item-1" to the second.
Examples:
Example 1
Add the class "selected" to the matched elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!doctype
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>
addClass demo
</
title
>
<
style
>
p
{
margin
:
8px
;
font-size
:
16px
;
}
.selected
{
color
: blue;
}
.highlight
{
background
: yellow;
}
</
style
>
<
script
src
=
"https://code.jquery.com/jquery-4.0.0.js"
>
</
script
>
</
head
>
<
body
>
<
p
>
Hello
</
p
>
<
p
>
and
</
p
>
<
p
>
Goodbye
</
p
>
<
script
>
$(
"p"
).last().addClass(
"selected"
);
</
script
>
</
body
>
</
html
>
Demo:
Example 2
Add the classes "selected" and "highlight" to the matched elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!doctype
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>
addClass demo
</
title
>
<
style
>
p
{
margin
:
8px
;
font-size
:
16px
;
}
.selected
{
color
: red;
}
.highlight
{
background
: yellow;
}
</
style
>
<
script
src
=
"https://code.jquery.com/jquery-4.0.0.js"
>
</
script
>
</
head
>
<
body
>
<
p
>
Hello
</
p
>
<
p
>
and
</
p
>
<
p
>
Goodbye
</
p
>
<
script
>
$(
"p"
).last().addClass(
"selected highlight"
);
</
script
>
</
body
>
</
html
>
Demo:
Example 3
Add the classes "selected" and "highlight" to the matched elements (3.3+ syntax).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!doctype
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>
addClass demo
</
title
>
<
style
>
p
{
margin
:
8px
;
font-size
:
16px
;
}
.selected
{
color
: red;
}
.highlight
{
background
: yellow;
}
</
style
>
<
script
src
=
"https://code.jquery.com/jquery-4.0.0.js"
>
</
script
>
</
head
>
<
body
>
<
p
>
Hello
</
p
>
<
p
>
and
</
p
>
<
p
>
Goodbye
</
p
>
<
script
>
$(
"p"
).last().addClass( [
"selected"
,
"highlight"
] );
</
script
>
</
body
>
</
html
>
Demo:
Example 4
Pass in a function to
.addClass()
to add the "green" class to a div that already has a "red" class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!doctype
html
>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"utf-8"
>
<
title
>
addClass demo
</
title
>
<
style
>
div
{
background
: white;
}
.red
{
background
: red;
}
.red
.green
{
background
: green;
}
</
style
>
<
script
src
=
"https://code.jquery.com/jquery-4.0.0.js"
>
</
script
>
</
head
>
<
body
>
<
div
>
This div should be white
</
div
>
<
div
class
=
"red"
>
This div will be green because it now has the "green" and "red" classes.
It would be red if the addClass function failed.
</
div
>
<
div
>
This div should be white
</
div
>
<
p
>
There are zero green divs
</
p
>
<
script
>
$(
"div"
).addClass(
function
(
index, currentClass
)
{
var
addedClass;
if
( currentClass ===
"red"
) {
addedClass =
"green"
;
$(
"p"
).text(
"There is one green div"
);
}
return
addedClass;
});
</
script
>
</
body
>
</
html
>
Demo: |
| Markdown | - [jQuery](https://jquery.com/ "jQuery")
- [jQuery UI](https://jqueryui.com/ "jQuery UI")
- [jQuery Mobile](https://jquerymobile.com/ "jQuery Mobile")
- [Sizzle](https://sizzlejs.com/ "Sizzle")
- [QUnit](https://qunitjs.com/ "QUnit")
- [Plugins](https://plugins.jquery.com/)
- [Contribute](https://contribute.jquery.org/)
- [CLA](https://cla.openjsf.org/)
- [Style Guides](https://contribute.jquery.org/style-guide/)
- [Bug Triage](https://contribute.jquery.org/triage/)
- [Code](https://contribute.jquery.org/code/)
- [Documentation](https://contribute.jquery.org/documentation/)
- [Web Sites](https://contribute.jquery.org/web-sites/)
- [Events](https://events.jquery.org/)
- [Support](https://jquery.com/support/)
- [Learning Center](https://learn.jquery.com/)
- [Chat](https://jquery.com/support/)
- [Stack Overflow](https://stackoverflow.com/tags/jquery/info)
- [Report a bug](https://contribute.jquery.org/bug-reports/)
- [OpenJS Foundation](https://openjsf.org/)
- [Join](https://openjsf.org/join)
- [Members](https://openjsf.org/members)
- [jQuery Team](https://jquery.com/team)
- [Governance](https://openjsf.org/governance)
- [Conduct](https://code-of-conduct.openjsf.org/)
- [Projects](https://openjsf.org/projects)
## [jQuery API Documentation](https://jquery.com/ "jQuery API Documentation")
Navigation
- [Download](https://jquery.com/download/)
- [API Documentation](https://api.jquery.com/)
- [Blog](https://blog.jquery.com/)
- [Plugins](https://plugins.jquery.com/)
- [Browser Support](https://jquery.com/browser-support/)
- [Version Support](https://jquery.com/support/)
# .addClass()
***
Categories: [Attributes](https://api.jquery.com/category/attributes/) \| [Manipulation](https://api.jquery.com/category/manipulation/) \> [Class Attribute](https://api.jquery.com/category/manipulation/class-attribute/) \| [CSS](https://api.jquery.com/category/css/)
## .addClass( className )Returns: [jQuery](http://api.jquery.com/Types/#jQuery)
**Description:** Adds the specified class(es) to each element in the set of matched elements.
- #### version added: [1\.0](https://api.jquery.com/category/version/1.0/)[.addClass( className )](https://api.jquery.com/addClass/#addClass-className)
- **className**
Type: [String](http://api.jquery.com/Types/#String)
One or more space-separated classes to be added to the class attribute of each matched element.
- #### version added: [3\.3](https://api.jquery.com/category/version/3.3/)[.addClass( classNames )](https://api.jquery.com/addClass/#addClass-classNames)
- **classNames**
Type: [Array](http://api.jquery.com/Types/#Array)
An array of classes to be added to the class attribute of each matched element.
- #### version added: [1\.4](https://api.jquery.com/category/version/1.4/)[.addClass( function )](https://api.jquery.com/addClass/#addClass-function)
- **function**
Type: [Function](http://api.jquery.com/Types/#Function)( [Integer](http://api.jquery.com/Types/#Integer) index, [String](http://api.jquery.com/Types/#String) currentClassName ) =\> [String](http://api.jquery.com/Types/#String)
A function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, `this` refers to the current element in the set.
- #### version added: [3\.3](https://api.jquery.com/category/version/3.3/)[.addClass( function )](https://api.jquery.com/addClass/#addClass-function)
- **function**
Type: [Function](http://api.jquery.com/Types/#Function)( [Integer](http://api.jquery.com/Types/#Integer) index, [String](http://api.jquery.com/Types/#String) currentClassName ) =\> [String](http://api.jquery.com/Types/#String) \| [Array](http://api.jquery.com/Types/#Array)
A function returning one or more space-separated class names or an array of class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, `this` refers to the current element in the set.
It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.
Before jQuery version 1.12/2.2, the `.addClass()` method manipulated the `className` *property* of the selected elements, not the `class` *attribute*. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).
As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the `class` *attribute* is used instead. So, `.addClass()` can be used on XML or SVG documents.
More than one class may be added at a time, separated by a space, to the set of matched elements, like so:
| | |
|---|---|
| 1 | |
This method is often used with `.removeClass()` to switch elements' classes from one to another, like so:
| | |
|---|---|
| 1 | |
Here, the `myClass` and `noClass` classes are removed from all paragraphs, while `yourClass` is added.
As of jQuery 1.4, the `.addClass()` method's argument can receive a function.
| | |
|---|---|
| 1 2 3 | |
Given an unordered list with two `<li>` elements, this example adds the class "item-0" to the first `<li>` and "item-1" to the second.
## Examples:
### Example 1
Add the class "selected" to the matched elements.
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
#### Demo:
### Example 2
Add the classes "selected" and "highlight" to the matched elements.
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
#### Demo:
### Example 3
Add the classes "selected" and "highlight" to the matched elements (3.3+ syntax).
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
#### Demo:
### Example 4
Pass in a function to `.addClass()` to add the "green" class to a div that already has a "red" class.
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
#### Demo:
- [Ajax](https://api.jquery.com/category/ajax/)
- [Global Ajax Event Handlers](https://api.jquery.com/category/ajax/global-ajax-event-handlers/)
- [Helper Functions](https://api.jquery.com/category/ajax/helper-functions/)
- [Low-Level Interface](https://api.jquery.com/category/ajax/low-level-interface/)
- [Shorthand Methods](https://api.jquery.com/category/ajax/shorthand-methods/)
- [Attributes](https://api.jquery.com/category/attributes/)
- [Callbacks Object](https://api.jquery.com/category/callbacks-object/)
- [Core](https://api.jquery.com/category/core/)
- [CSS](https://api.jquery.com/category/css/)
- [Data](https://api.jquery.com/category/data/)
- [Deferred Object](https://api.jquery.com/category/deferred-object/)
- [Deprecated](https://api.jquery.com/category/deprecated/)
- [Deprecated 1.3](https://api.jquery.com/category/deprecated/deprecated-1.3/)
- [Deprecated 1.7](https://api.jquery.com/category/deprecated/deprecated-1.7/)
- [Deprecated 1.8](https://api.jquery.com/category/deprecated/deprecated-1.8/)
- [Deprecated 1.9](https://api.jquery.com/category/deprecated/deprecated-1.9/)
- [Deprecated 1.10 & 2.0](https://api.jquery.com/category/deprecated/deprecated-1.10-and-2.0/)
- [Deprecated 3.0](https://api.jquery.com/category/deprecated/deprecated-3.0/)
- [Deprecated 3.2](https://api.jquery.com/category/deprecated/deprecated-3.2/)
- [Deprecated 3.3](https://api.jquery.com/category/deprecated/deprecated-3.3/)
- [Deprecated 3.4](https://api.jquery.com/category/deprecated/deprecated-3.4/)
- [Deprecated 3.5](https://api.jquery.com/category/deprecated/deprecated-3.5/)
- [Deprecated 3.7](https://api.jquery.com/category/deprecated/deprecated-3.7/)
- [Dimensions](https://api.jquery.com/category/dimensions/)
- [Effects](https://api.jquery.com/category/effects/)
- [Basics](https://api.jquery.com/category/effects/basics/)
- [Custom](https://api.jquery.com/category/effects/custom-effects/)
- [Fading](https://api.jquery.com/category/effects/fading/)
- [Sliding](https://api.jquery.com/category/effects/sliding/)
- [Events](https://api.jquery.com/category/events/)
- [Browser Events](https://api.jquery.com/category/events/browser-events/)
- [Document Loading](https://api.jquery.com/category/events/document-loading/)
- [Event Handler Attachment](https://api.jquery.com/category/events/event-handler-attachment/)
- [Event Object](https://api.jquery.com/category/events/event-object/)
- [Form Events](https://api.jquery.com/category/events/form-events/)
- [Keyboard Events](https://api.jquery.com/category/events/keyboard-events/)
- [Mouse Events](https://api.jquery.com/category/events/mouse-events/)
- [Forms](https://api.jquery.com/category/forms/)
- [Internals](https://api.jquery.com/category/internals/)
- [Manipulation](https://api.jquery.com/category/manipulation/)
- [Class Attribute](https://api.jquery.com/category/manipulation/class-attribute/)
- [Copying](https://api.jquery.com/category/manipulation/copying/)
- [DOM Insertion, Around](https://api.jquery.com/category/manipulation/dom-insertion-around/)
- [DOM Insertion, Inside](https://api.jquery.com/category/manipulation/dom-insertion-inside/)
- [DOM Insertion, Outside](https://api.jquery.com/category/manipulation/dom-insertion-outside/)
- [DOM Removal](https://api.jquery.com/category/manipulation/dom-removal/)
- [DOM Replacement](https://api.jquery.com/category/manipulation/dom-replacement/)
- [General Attributes](https://api.jquery.com/category/manipulation/general-attributes/)
- [Style Properties](https://api.jquery.com/category/manipulation/style-properties/)
- [Miscellaneous](https://api.jquery.com/category/miscellaneous/)
- [Collection Manipulation](https://api.jquery.com/category/miscellaneous/collection-manipulation/)
- [Data Storage](https://api.jquery.com/category/miscellaneous/data-storage/)
- [DOM Element Methods](https://api.jquery.com/category/miscellaneous/dom-element-methods/)
- [Setup Methods](https://api.jquery.com/category/miscellaneous/setup-methods/)
- [Offset](https://api.jquery.com/category/offset/)
- [Properties](https://api.jquery.com/category/properties/)
- [Properties of jQuery Object Instances](https://api.jquery.com/category/properties/jquery-object-instance-properties/)
- [Properties of the Global jQuery Object](https://api.jquery.com/category/properties/global-jquery-object-properties/)
- [Removed](https://api.jquery.com/category/removed/)
- [Selectors](https://api.jquery.com/category/selectors/)
- [Attribute](https://api.jquery.com/category/selectors/attribute-selectors/)
- [Basic](https://api.jquery.com/category/selectors/basic-css-selectors/)
- [Basic Filter](https://api.jquery.com/category/selectors/basic-filter-selectors/)
- [Child Filter](https://api.jquery.com/category/selectors/child-filter-selectors/)
- [Content Filter](https://api.jquery.com/category/selectors/content-filter-selector/)
- [Form](https://api.jquery.com/category/selectors/form-selectors/)
- [Hierarchy](https://api.jquery.com/category/selectors/hierarchy-selectors/)
- [jQuery Extensions](https://api.jquery.com/category/selectors/jquery-selector-extensions/)
- [Visibility Filter](https://api.jquery.com/category/selectors/visibility-filter-selectors/)
- [Traversing](https://api.jquery.com/category/traversing/)
- [Filtering](https://api.jquery.com/category/traversing/filtering/)
- [Miscellaneous Traversing](https://api.jquery.com/category/traversing/miscellaneous-traversal/)
- [Tree Traversal](https://api.jquery.com/category/traversing/tree-traversal/)
- [Utilities](https://api.jquery.com/category/utilities/)
- [Version](https://api.jquery.com/category/version/)
- [Version 1.0](https://api.jquery.com/category/version/1.0/)
- [Version 1.0.4](https://api.jquery.com/category/version/1.0.4/)
- [Version 1.1](https://api.jquery.com/category/version/1.1/)
- [Version 1.1.2](https://api.jquery.com/category/version/1.1.2/)
- [Version 1.1.3](https://api.jquery.com/category/version/1.1.3/)
- [Version 1.1.4](https://api.jquery.com/category/version/1.1.4/)
- [Version 1.2](https://api.jquery.com/category/version/1.2/)
- [Version 1.2.3](https://api.jquery.com/category/version/1.2.3/)
- [Version 1.2.6](https://api.jquery.com/category/version/1.2.6/)
- [Version 1.3](https://api.jquery.com/category/version/1.3/)
- [Version 1.4](https://api.jquery.com/category/version/1.4/)
- [Version 1.4.1](https://api.jquery.com/category/version/1.4.1/)
- [Version 1.4.2](https://api.jquery.com/category/version/1.4.2/)
- [Version 1.4.3](https://api.jquery.com/category/version/1.4.3/)
- [Version 1.4.4](https://api.jquery.com/category/version/1.4.4/)
- [Version 1.5](https://api.jquery.com/category/version/1.5/)
- [Version 1.5.1](https://api.jquery.com/category/version/1.5.1/)
- [Version 1.6](https://api.jquery.com/category/version/1.6/)
- [Version 1.7](https://api.jquery.com/category/version/1.7/)
- [Version 1.8](https://api.jquery.com/category/version/1.8/)
- [Version 1.9](https://api.jquery.com/category/version/1.9/)
- [Version 1.11 & 2.1](https://api.jquery.com/category/version/1.11-and-2.1/)
- [Version 1.12 & 2.2](https://api.jquery.com/category/version/1.12-and-2.2/)
- [Version 3.0](https://api.jquery.com/category/version/3.0/)
- [Version 3.1](https://api.jquery.com/category/version/3.1/)
- [Version 3.2](https://api.jquery.com/category/version/3.2/)
- [Version 3.3](https://api.jquery.com/category/version/3.3/)
- [Version 3.4](https://api.jquery.com/category/version/3.4/)
- [Version 3.5](https://api.jquery.com/category/version/3.5/)
- [Version 3.6](https://api.jquery.com/category/version/3.6/)
- [Version 3.7](https://api.jquery.com/category/version/3.7/)
- [Version 4.0](https://api.jquery.com/category/version/4.0/)
### Books
- [ Learning jQuery Fourth Edition Karl Swedberg and Jonathan Chaffer](https://www.packtpub.com/en-us/product/learning-jquery-fourth-edition-9781782163152)
- [ jQuery in Action Bear Bibeault, Yehuda Katz, and Aurelio De Rosa](https://www.manning.com/books/jquery-in-action-third-edition)
- [ jQuery Succinctly Cody Lindley](https://www.syncfusion.com/ebooks/jquery)
- [Learning Center](https://learn.jquery.com/)
- [Chat](https://jquery.com/support/)
- [Twitter](https://twitter.com/jquery)
- [GitHub](https://github.com/jquery)
Copyright 2026 [OpenJS Foundation](https://openjsf.org/) and jQuery contributors. All rights reserved. See [jQuery License](https://jquery.com/license/) for more information. The [OpenJS Foundation](https://openjsf.org/) has registered trademarks and uses trademarks. For a list of trademarks of the [OpenJS Foundation](https://openjsf.org/), please see our [Trademark Policy](https://trademark-policy.openjsf.org/) and [Trademark List](https://trademark-list.openjsf.org/). Trademarks and logos not indicated on the [list of OpenJS Foundation trademarks](https://trademark-list.openjsf.org/) are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. OpenJS Foundation [Terms of Use](https://terms-of-use.openjsf.org/), [Privacy](https://privacy-policy.openjsf.org/), [Code of Conduct](https://code-of-conduct.openjsf.org/), and [Cookie](https://www.linuxfoundation.org/cookies) Policies also apply.
[Web hosting by Digital Ocean](https://www.digitalocean.com/) \| [CDN by Fastly](https://www.fastly.com/) \| [Powered by WordPress](https://wordpress.org/) |
| Readable Markdown | ## .addClass( className )Returns: [jQuery](http://api.jquery.com/Types/#jQuery)
**Description:** Adds the specified class(es) to each element in the set of matched elements.
- #### version added: [1\.0](https://api.jquery.com/category/version/1.0/)[.addClass( className )](https://api.jquery.com/addClass/#addClass-className)
- **className**
One or more space-separated classes to be added to the class attribute of each matched element.
- #### version added: [3\.3](https://api.jquery.com/category/version/3.3/)[.addClass( classNames )](https://api.jquery.com/addClass/#addClass-classNames)
- **classNames**
An array of classes to be added to the class attribute of each matched element.
- #### version added: [1\.4](https://api.jquery.com/category/version/1.4/)[.addClass( function )](https://api.jquery.com/addClass/#addClass-function)
- **function**
A function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, `this` refers to the current element in the set.
- #### version added: [3\.3](https://api.jquery.com/category/version/3.3/)[.addClass( function )](https://api.jquery.com/addClass/#addClass-function)
- **function**
A function returning one or more space-separated class names or an array of class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, `this` refers to the current element in the set.
It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.
Before jQuery version 1.12/2.2, the `.addClass()` method manipulated the `className` *property* of the selected elements, not the `class` *attribute*. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).
As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the `class` *attribute* is used instead. So, `.addClass()` can be used on XML or SVG documents.
More than one class may be added at a time, separated by a space, to the set of matched elements, like so:
| | |
|---|---|
| 1 | |
This method is often used with `.removeClass()` to switch elements' classes from one to another, like so:
| | |
|---|---|
| 1 | |
Here, the `myClass` and `noClass` classes are removed from all paragraphs, while `yourClass` is added.
As of jQuery 1.4, the `.addClass()` method's argument can receive a function.
| | |
|---|---|
| 1 2 3 | |
Given an unordered list with two `<li>` elements, this example adds the class "item-0" to the first `<li>` and "item-1" to the second.
## Examples:
### Example 1
Add the class "selected" to the matched elements.
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
#### Demo:
### Example 2
Add the classes "selected" and "highlight" to the matched elements.
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
#### Demo:
### Example 3
Add the classes "selected" and "highlight" to the matched elements (3.3+ syntax).
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
#### Demo:
### Example 4
Pass in a function to `.addClass()` to add the "green" class to a div that already has a "red" class.
| | |
|---|---|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
#### Demo: |
| Shard | 53 (laksa) |
| Root Hash | 657907979407719853 |
| Unparsed URL | com,jquery!api,/addClass/ s443 |