jQuery API

.toggle()

.toggle( handler(eventObject), handler(eventObject) [, handler(eventObject)] ) Returns: jQuery

Description: Bind two or more handlers to the matched elements, to be executed on alternate clicks.

  • version added: 1.0.toggle( handler(eventObject), handler(eventObject) [, handler(eventObject)] )

    handler(eventObject)A function to execute every even time the element is clicked.

    handler(eventObject)A function to execute every odd time the element is clicked.

    handler(eventObject)Additional handlers to cycle through after clicks.

The .toggle() method binds a handler for the click event, so the rules outlined for the triggering of click apply here as well.

For example, consider the HTML:
<div id="target">
  Click here
</div>

Event handlers can then be bound to the <div>:

$('#target').toggle(function() {
  alert('First handler for .toggle() called.');
}, function() {
  alert('Second handler for .toggle() called.');
});

As the element is clicked repeatedly, the messages alternate:

First handler for .toggle() called.
Second handler for .toggle() called.
First handler for .toggle() called.
Second handler for .toggle() called.
First handler for .toggle() called.

If more than two handlers are provided, .toggle() will cycle among all of them. For example, if there are three handlers, then the first handler will be called on the first click, the fourth click, the seventh click, and so on.

Note: jQuery also provides an animation method named .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of arguments passed.

The .toggle() method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into .toggle() prove limiting. For example, .toggle() is not guaranteed to work correctly if applied twice to the same element. Since .toggle() internally uses a click handler to do its work, we must unbind click to remove a behavior attached with .toggle(), so other click handlers can be caught in the crossfire. The implementation also calls .preventDefault() on the event, so links will not be followed and buttons will not be clicked if .toggle() has been called on the element.

Examples:

Example: Click to toggle highlight on the list item.

<!DOCTYPE html>
<html>
<head>
  <style>
  ul { margin:10px; list-style:inside circle; font-weight:bold; }
  li { cursor:pointer; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <ul>
    <li>Go to the store</li>
    <li>Pick up dinner</li>
    <li>Debug crash</li>

    <li>Take a jog</li>
  </ul>
<script>
    $("li").toggle(
      function () {
        $(this).css({"list-style-type":"disc", "color":"blue"});
      },
      function () {
        $(this).css({"list-style-type":"disc", "color":"red"});
      },
      function () {
        $(this).css({"list-style-type":"", "color":""});
      }
    );

</script>

</body>
</html>

Demo:

Example: To toggle a style on table cells:

$("td").toggle(
  function () {
    $(this).addClass("selected");
  },
  function () {
    $(this).removeClass("selected");
  }
);

Support and Contributions

Need help with .toggle() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .toggle()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • https://launchpad.net/~melat0nin melat0nin

    It's potentially confusing this having the same name as the animation function.. had me scratching my head for a while when someone suggested I use this to change some image states…

  • http://www.learningjquery.com/ Karl Swedberg

    Yes, it is. But, as with many other less-than-ideal API decisions that were made a number of years ago, it would also be potentially frustrating to many developers whose code would break in bad ways if we changed the API at this point. Changing method names that have been in use for years is not something that we take lightly.

  • Will Eizlini

    if you call $(“#element”).toggle(functionExpand,functionContract) twice (perhaps by accident), then each click will call the functionExpand and functionContract. To make sure that toggle is called only once you will have to perform some kind of check. One solution, although perhaps not the most elegant is something like this:

    if($(“#element”).data(“init”!=”true”)
    {
    $(#element”).toggle(function(){alert('expand');},function(){alert('contract');})
    $(#element”).data(“init”,”true”);
    }

  • Will Eizlini

    sorry this comment has the correct code :

    if you call $(“#element”).toggle(functionExpand,functionContract) twice (perhaps by accident), then each click will call the functionExpand and functionContract. To make sure that toggle is called only once you will have to perform some kind of check. One solution, although perhaps not the most elegant is something like this:

    if($(“#element”).data(“init”)!=”true”)
    {
    $(#element”).toggle(function(){alert('expand');},function(){alert('contract');})
    $(#element”).data(“init”,”true”);
    }

  • Pimus

    can i do that?

    if($('#midiv').toggle() =='hide' )

    do some thing

    else

    do other think

  • anthony

    For some reason this doesnt seem to be working in IE8????