.toggleClass( className )Returns: jQuery
Description: Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.
-
version added: 1.0.toggleClass( className )
-
classNameType: StringOne or more class names (separated by spaces) to be toggled for each element in the matched set.
-
-
version added: 1.3.toggleClass( className, state )
-
version added: 1.4.toggleClass( [state ] )
-
stateType: BooleanA boolean value to determine whether the class should be added or removed.
-
-
version added: 1.4.toggleClass( function [, state ] )
-
functionA function that returns class names to be toggled in the class attribute of each element in the matched set. Receives the index position of the element in the set, the old class value, and the state as arguments.
-
stateType: BooleanA boolean value to determine whether the class should be added or removed.
-
Before jQuery version 1.12/2.2, the .toggleClass()
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, .toggleClass()
can be used on XML or SVG documents.
This method takes one or more class names as its parameter. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added. For example, we can apply .toggleClass()
to a simple <div>
:
1
|
|
The first time we apply $( "div.tumble" ).toggleClass( "bounce" )
, we get the following:
1
|
|
The second time we apply $( "div.tumble" ).toggleClass( "bounce" )
, the <div>
class is returned to the single tumble
value:
1
|
|
Applying .toggleClass( "bounce spin" )
to the same <div>
alternates between <div class="tumble bounce spin">
and <div class="tumble">
.
The second version of .toggleClass()
uses the second parameter for determining whether the class should be added or removed. If this parameter's value is true
, then the class is added; if false
, the class is removed. In essence, the statement:
1
|
|
is equivalent to:
1
2
3
4
5
|
|
As of jQuery 1.4, if no arguments are passed to .toggleClass()
, all class names on the element the first time .toggleClass()
is called will be toggled. Also as of jQuery 1.4, the class name to be toggled can be determined by passing in a function.
1
2
3
4
5
6
7
|
|
This example will toggle the happy
class for <div class="foo">
elements if their parent element has a class of bar
; otherwise, it will toggle the sad
class.
Examples:
Toggle the class 'highlight' when a paragraph is clicked.
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
|
|
Demo:
Add the "highlight" class to the clicked paragraph on every third click of that paragraph, remove it every first and second click.
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
42
43
|
|
Demo:
Toggle the class name(s) indicated on the buttons for each div.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
|