Example

Toggle between hide and show for a p element:

$("button").click(function(){
  $("p").toggle();
});

Try it yourself »

Definition and Usage

The toggle() method toggles between hide() and show() for the selected elements.

This method is also used to toggle between custom functions.


Toggle Between Hide() and Show()

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.

Syntax

$(selector).toggle(speed,callback)

Parameter Description
speed Optional. Specifies the speed of the hide/show effect. Default is "0".

Possible values:

  • milliseconds (like 1500)
  • "slow"
  • "normal"
  • "fast"
callback Optional. A function to be executed after the toggle() method is completed.

To learn more about callback, visit our jQuery Callback chapter.



Show or Hide Elements

Specifies whether to ONLY show or ONLY hide ALL matched elements.

Syntax

$(selector).toggle(switch)
Try it yourself »

Parameter Description
switch Required. A Boolean value that specifies if toggle() should only show or only hide all selected elements.
  • true - show elements
  • false - hide elements


Toggle Between Functions

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.

Syntax

$(selector).toggle(function(),function(),function(),...)
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


Examples

Try it Yourself - Examples

Using the speed parameter
How to use the speed parameter to specify the speed of the hide/show effect.