The jQuery Toggle method (.toggle())will toggle between hide() and show() properties for the selected elements. This means if an element is hidden then .toggle() will show it, similarly if the element is showing then .toggle will hide it.
$(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 easily 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.
$("#div").toggle("slow", "linear", function () {
alert("Toggling is finished!");
});
Download source codes: