jQuery fadeToggle() Method

Last Updated:

jQuery fadeToggle() Method

The jQuery fadeToggle() method will display or hide the matched elements by animating their opacity.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeToggle() Method</h1> <button>Fade Toggle</button> <script> $(document).ready(function(){ $("button").click(function(){ $("p").fadeToggle(); }); }); </script> </body> </html>

Syntax

$(selector).fadeToggle(speed, easing, callback);

Parameter Values

ValueTypeExplanation
speed Required Specifies the speed of the fading effect.
Possible values are
  • "slow"
  • "fast"
  • milliseconds
easing Optional Specifies the easing function to use for the transition.
Default value is "swing"
Possible values are
  • "swing"
  • "linear"
callback Optional Specifies a function to be called once the animation is completed.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
fadeToggle()YesYesYesYesYesYesYesYesYesYesYesYesYes

fadeToggle() with Speed

In the following example, we will specify the speed of the fading effect for fadeToggle() method to 2000ms (2 seconds).

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeToggle() Method</h1> <button>Fade Toggle</button> <script> $(document).ready(function(){ $("button").click(function(){ $("p").fadeToggle(2000); }); }); </script> </body> </html>

fadeToggle() with Easing

In the following example, we will specify the easing function for fadeToggle() method to linear.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeToggle() Method</h1> <button>Fade Toggle</button> <script> $(document).ready(function(){ $("button").click(function(){ $("p").fadeToggle(2000, "linear"); }); }); </script> </body> </html>

fadeToggle() with Callback

In the following example, we will call a function (myFunction) once the fading effect for fadeToggle() method is finished.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeToggle() Method</h1> <button>Fade Toggle</button> <script> $(document).ready(function(){ $("button").click(function(){ $("p").fadeToggle(2000, "linear", myFunction); }); }); function myFunction(){ alert("Toggle is completed"); } </script> </body> </html>

Share this Page

Meet the Author