.toggle()

Display or hide the matched elements.

Note: The event handling suite also has a method named .toggle(). Which one is fired depends on the set of arguments passed.

With no parameters, the .toggle() method simply toggles the visibility of elements:

$( ".target" ).toggle();

The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

When a duration, a plain object, or a single "complete" function is provided, .toggle() becomes an animation method. The .toggle() method animates the width, height, and opacity of the matched elements simultaneously. When these properties reach 0 after a hiding animation, the display style property is set to none to ensure that the element no longer affects the layout of the page.

Durations are given in milliseconds; higher values indicate slower animations, not faster ones. The strings 'fast' and 'slow' can be supplied to indicate durations of 200 and 600 milliseconds, respectively.

As of jQuery 1.4.3, an optional string naming an easing function may be used. Easing functions specify the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a constant pace, called linear. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.

If supplied, the callback is fired once the animation is complete. This can be useful for stringing different animations together in sequence. The callback is not sent any arguments, but this is set to the DOM element being animated. If multiple elements are animated, it is important to note that the callback is executed once per matched element, not once for the animation as a whole.

We can animate any element, such as a simple image:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123">

We will cause .toggle() to be called when another element is clicked:

$( "#clickme" ).click(function() {
  $( "#book" ).toggle( "slow", function() {
    // Animation complete.
  });
});

With the element initially shown, we can hide it slowly with the first click:

figure 1

A second click will show the element once again:

figure 2

The second version of the method accepts a Boolean parameter. If this parameter is true, then the matched elements are shown; if false, the elements are hidden. In essence, the statement:

$( "#foo" ).toggle( display );

is equivalent to:

if ( display === true ) {
  $( "#foo" ).show();
} else if ( display === false ) {
  $( "#foo" ).hide();
}
  • All jQuery effects, including .toggle(), can be turned off globally by setting jQuery.fx.off = true, which effectively sets the duration to 0. For more information, see jQuery.fx.off.
version added: 1.0
duration (default: 400)

A string or number determining how long the animation will run.

complete

A function to call once the animation is complete, called once per matched element.

version added: 1.0
options

A map of additional options to pass to the method.

duration (default: 400)

A string or number determining how long the animation will run.

easing (default: swing)

A string indicating which easing function to use for the transition.

queue (default: true)

A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. As of jQuery 1.7, the queue option can also accept a string, in which case the animation is added to the queue represented by that string. When a custom queue name is used the animation does not automatically start; you must call .dequeue("queuename") to start it.

specialEasing

An object containing one or more of the CSS properties defined by the properties argument and their corresponding easing functions. (version added: 1.4)

step
Function( Number now, Tween tween )

A function to be called for each animated property of each animated element. This function provides an opportunity to modify the Tween object to change the value of the property before it is set.

progress
Function( Promise animation, Number progress, Number remainingMs )

A function to be called after each step of the animation, only once per animated element regardless of the number of animated properties. (version added: 1.8)

complete

A function that is called once the animation on an element is complete.

start
Function( Promise animation )

A function to call when the animation on an element begins. (version added: 1.8)

done
Function( Promise animation, Boolean jumpedToEnd )

A function to be called when the animation on an element completes (its Promise object is resolved). (version added: 1.8)

fail
Function( Promise animation, Boolean jumpedToEnd )

A function to be called when the animation on an element fails to complete (its Promise object is rejected). (version added: 1.8)

always
Function( Promise animation, Boolean jumpedToEnd )

A function to be called when the animation on an element completes or stops without completing (its Promise object is either resolved or rejected). (version added: 1.8)

version added: 1.4.3
duration (default: 400)

A string or number determining how long the animation will run.

easing (default: swing)

A string indicating which easing function to use for the transition.

complete

A function to call once the animation is complete, called once per matched element.

version added: 1.3
display

Use true to show the element or false to hide it.

Examples:

Toggles all paragraphs.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>toggle demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<button>Toggle</button>
<p>Hello</p>
<p style="display: none">Good Bye</p>
 
<script>
$( "button" ).click(function() {
  $( "p" ).toggle();
});
</script>
 
</body>
</html>

Animates all paragraphs to be shown if they are hidden and hidden if they are visible, completing the animation within 600 milliseconds.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>toggle demo</title>
  <style>
  p {
    background: #dad;
    font-weight: bold;
    font-size: 16px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<button>Toggle 'em</button>
<p>Hiya</p>
<p>Such interesting text, eh?</p>
 
<script>
$( "button" ).click(function() {
  $( "p" ).toggle( "slow" );
});
</script>
 
</body>
</html>

Shows all paragraphs, then hides them all, back and forth.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>toggle demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<button>Toggle</button>
<p>Hello</p>
<p style="display: none">Good Bye</p>
 
<script>
var flip = 0;
$( "button" ).click(function() {
  $( "p" ).toggle( flip++ % 2 === 0 );
});
</script>
 
</body>
</html>
doc_jQuery
2016-03-27 13:49:08
Comments
Leave a Comment

Please login to continue.