
The jQuery Toggle method (.toggle()) switches the selected elements between hidden and visible. If an element is hidden, .toggle() shows it, and if the element is visible, .toggle() hides it. It works like calling .show() or .hide(), depending on the element’s current state.
$(selector).toggle(speed,easing,callback)
| Parameter | Description |
|---|---|
| speed | Optional value. Specifies the speed of the toggle effect. It can be:
|
| easing | Optional value. Specifies the type of speed of the element during the toggling. Default value is “swing”. It can be:
|
| callback | Optional. A function to be executed after the toggle method is completed. |
The following code toggles a div element with id called div1.
$("#div1").toggle();
The code below toggles the div in 2 seconds.
$("#div1").toggle(2000);
Here I have passed the speed and easing parameters to the jQuery Toggle method.
$("#div1").toggle("slow", "linear");
In this code I have also passed a callback function that will show the alert message after the toggling is finished.
$("#div1").toggle("slow", "linear", function () {
alert("Toggling is finished!");
});
Download source codes: