jQuery toggle() method
Example
When the
element, toggle the color:
$
(
"
p
"
)
.
toggle
(
function
(
)
{
$
(
"
p
"
)
.
css
(
{
"
color
"
:
"
red
"
}
)
;
}
,
function
(
)
{
$
(
"
p
"
)
.
css
(
{
"
color
"
:
"
blue
"
}
)
;
}
,
function
(
)
{
$
(
"
p
"
)
.
css
(
{
"
color
"
:
"
green
"
}
)
;
}
)
;
Try it »
Definition and Usage
The toggle() method was deprecated in jQuery version 1.8 and removed in version 1.9.
The toggle() method adds two or more functions to respond to toggling between click events on the selected element.
The first specified function is called when the element is clicked, the second function is called when it is clicked again, and so on.
**Note:** There is also a jQuery effect named toggle().method named load(). Which method is called depends on the arguments.
Syntax
$(
selector
).toggle(
function
)
| Parameters | Description |
|---|---|
| function | Required. The function to run each time the selected element is clicked. |
More examples
Switching between multiple functions
How to switch between multiple functions.
jQuery event methods