.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 )
-
classNameType: StringOne or more space-separated classes to be added to the class attribute of each matched element.
-
-
version added: 3.3.addClass( classNames )
-
classNamesType: ArrayAn array of classes to be added to the class attribute of each matched element.
-
-
version added: 1.4.addClass( function )
-
functionA 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 )
-
functionA 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.
-
.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).
class
attribute is used instead. So, .addClass()
can be used on XML or SVG documents.
1
|
|
.removeClass()
to switch elements' classes from one to another, like so:
1
|
|
myClass
and noClass
classes are removed from all paragraphs, while yourClass
is added.
.addClass()
method's argument can receive a function.
1
2
3
|
|
<li>
elements, this example adds the class "item-0" to the first <li>
and "item-1" to the second.
Examples:
Example 1
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
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
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
.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
|
|