.toggle( handler, handler [, handler ] )Returns: jQueryversion deprecated: 1.8, removed: 1.9
Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks.
-
version added: 1.0.toggle( handler, handler [, handler ] )
-
handlerA function to execute every even time the element is clicked.
-
handlerA function to execute every odd time the element is clicked.
-
handlerAdditional handlers to cycle through after clicks.
-
.toggle()
method binds a handler for the click
event, so the rules outlined for the triggering of click
apply here as well.
1
2
3
|
|
<div>
:
1
2
3
4
5
|
|
Second handler for .toggle() called.
First handler for .toggle() called.
Second handler for .toggle() called.
First handler for .toggle() called.
.toggle()
will cycle among all of them. For example, if there are three handlers, then the first handler will be called on the first click, the fourth click, the seventh click, and so on.
.toggle()
method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into .toggle()
prove limiting. For example, .toggle()
is not guaranteed to work correctly if applied twice to the same element. Since .toggle()
internally uses a click
handler to do its work, we must unbind click
to remove a behavior attached with .toggle()
, so other click
handlers can be caught in the crossfire. The implementation also calls .preventDefault()
on the event, so links will not be followed and buttons will not be clicked if .toggle()
has been called on the element.
Example:
1
2
3
4
5
6
7
|
|