ExampleToggle between different background colors:
Try it yourself » |
The toggle() method is used to bind two or more event handler functions to toggle between for the click event for selected elements.
This method can also be used to toggle between hide() and show() for the selected elements.
Toggle between two or more functions when the specified element is clicked.
Note: If more than two functions are specified, the toggle() method will toggle between all of them. For example, if there are three functions, the first function will be called on the first click, the second function on the second click, the third function on the third click. On the fourth click the first function will be called again, and so on.
|
Try it yourself » |
Parameter | Description |
---|---|
function() | Required. Specifies a function to run every EVEN time the element is clicked |
function() | Required. Specifies a function to run every ODD time the element is clicked |
function(),... | Optional. Specifies additional functions to toggle between |
Checks each element for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.
$(selector).toggle(speed,callback) |
Parameter | Description |
---|---|
speed | Optional. Specifies the speed of the hide/show effect. Default is "0". Possible values:
|
callback | Optional. A function to be executed after the
toggle() method is completed. To learn more about callback, visit our jQuery Callback chapter. |
Specifies whether to ONLY show or ONLY hide ALL matched elements.
|
Try it yourself » |
Parameter | Description |
---|---|
switch | Required. A Boolean value that specifies if toggle() should
only show or only hide all selected elements.
|
![]() |
Try it Yourself - Examples |
Using the speed parameter
How to use the speed parameter to specify the speed of the hide/show effect.