W3Schools.com

jQuery Event toggle() Method

jQuery Event Methods jQuery Event Methods

Example

Toggle between different background colors:

$("p").toggle(function(){
  $("body").css("background-color","green");},
  function(){
  $("body").css("background-color","red");},
  function(){
  $("body").css("background-color","yellow");}
);

Try it yourself »

Definition and Usage

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.


Bind Two or More Functions to a Toggle Event

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


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


Examples

Try it Yourself - Examples

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


jQuery Event Methods jQuery Event Methods


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
Download XML Editor
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE