jQuery fadeToggle()

The jQuery fadeToggle() method is used to switch between the fadeIn and fadeOut states.

  • If the HTML element is hidden or already faded out, fadeToggle() method makes them fade in.
  • If the HTML element is visible or already faded in, fadeToggle() method makes them fade out.

In this tutorial, you will learn about jQuery fadeToggle() method, its syntax and usage, with examples.

Syntax – jQuery fadeToggle()

Following is the syntax to use jQuery fadeToggle() method

ADVERTISEMENT

1. jQuery fadeToggle – Default

To fade out a selected HTML Element with default timing,

$("htmlElement").fadeToggle();

2. jQuery fadeToggle – slow/fast

You can also provide a “slow” or “fast” argument to the fadeToggle() method to make the fade-in/fade-out process slow or fast respectively.

$("htmlElement").fadeToggle("slow");
$("htmlElement").fadeOut("fast");

3. jQuery fadeToggle – Time in milliseconds

To control the timing precisely, you can provide time in milliseconds as the argument to jQuery fadeOut() method.

$("htmlElement").fadeToggle(4000); // for 4000 milliseconds

4. jQuery fadeToggle – Callback Function

You can also provide an optional callback function as second argument to fadeToggle() method, to execute when the fade in or fade out effect is completed on the HTML element.

$("htmlElement").fadeToggle(4000, function(){ alert("Fading completed.") }); // for 4000 milliseconds

Examples – jQuery fadeToggle()

The following examples help you understand the usage of jQuery fadeToggle() method.

1. Example – Basic jQuery fadeToggle()

Following example illustrates the basic usage of jQuery fadeToggle() method.

2. Example – jQuery fadeToggle(“slow”), fadeToggle(“fast”)

You can provide one of the two options “slow”, “fast” to control the speed of the effect.

3. Example – jQuery fadeToggle(time_in_milliseconds)

You can control the fading speed at the precision of milliseconds. Provide a value in milliseconds as first argument.

4. Example – jQuery fadeToggle with Callback function

Callback function is an optional second argument. The callback function is called when the fading in/out effect is completed. In the following example, we have provided a callback function with alert statement inside the body. You can provide your own statements inside the callback function.

Conclusion

In this jQuery Tutorial, we have learnt jQuery fadeToggle() method : Syntax and Usage with examples.