11

Now that toggle(...) was deprecated in jQuery 1.8 and then removed in jQuery 1.9

What could be used in general (aside from using the jQuery migrate script) instead of toggle(fn, fn2); thats has the same type of functionality?

Related question (asked about a specific case): What to use instead toggle?


I know that toggle() functionality was not removed, just the ability to add custom toggle functions (aside from the show/hide default functionality).

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • is slideToggle still valid? – Lowkase Jan 17 '13 at 15:52
  • @Lokase I do not see why it would not be? – Naftali Jan 17 '13 at 15:52
  • Just implement it yourself if you use it often, here are a few examples: https://forum.jquery.com/topic/beginner-function-toggle-deprecated-what-to-use-instead#14737000003769261 I find it much easier to understand if it's hidden behind a function rather than having counter vars all over the place. – Kevin B Jan 17 '13 at 15:52
  • There are a whole bunch of alternative toggles, just use the one that best applies to the animation you are shooting for. – Lowkase Jan 17 '13 at 15:55
  • Curious that the relevant deprecation notes (http://bugs.jquery.com/ticket/11786) doesn't hint at a suitable replacement. dmethvin's comment (http://bugs.jquery.com/ticket/11786#comment:7) only alludes to Migrate. – Jeromy French Jan 17 '13 at 15:55
  • 1
    It's one of those methods where it has a great purpose, but it's named inconveniently. The method was widely used and does save on code, but the .toggle() show hide is used far more, so the former was removed to simplify the api. – Kevin B Jan 17 '13 at 15:57
  • 1
    @Lokase: The toggle *event* (http://api.jquery.com/toggle-event/) has been deprecated; the toggle *effect* (http://api.jquery.com/toggle/) has **not** been deprecated. The ambiguity is part of the reason the event is going/gone away (http://bugs.jquery.com/ticket/11786#comment:1) – Jeromy French Jan 17 '13 at 15:58

1 Answers1

20

Here is a simple implementation:

$.fn.toggleClick = function() {
    var methods = arguments;    // Store the passed arguments for future reference
    var count = methods.length; // Cache the number of methods 

    // Use return this to maintain jQuery chainability
    // For each element you bind to
    return this.each(function(i, item){
        // Create a local counter for that element
        var index = 0;

        // Bind a click handler to that element
        $(item).on('click', function() {
            // That when called will apply the 'index'th method to that element
            // the index % count means that we constrain our iterator between 0
            // and (count-1)
            return methods[index++ % count].apply(this, arguments);
        });
    });
};

and use it as

$('selector').toggleClick( function1, function2, ... );
Naftali
  • 144,921
  • 39
  • 244
  • 303
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • I **like** it. Can you add some comments explaining it all to people? ^_^ – Naftali Jan 17 '13 at 16:10
  • @Neal, made an update because i had forgotten to `return this` to maintain chaining.. – Gabriele Petrioli Jan 17 '13 at 16:18
  • Why do you need the `return` in the click handler? – Naftali Jan 17 '13 at 16:20
  • @Neal, so that we respect what the user does with their passed methods. For instance if applied to links, the user might do a `return false` at the end, and we want to pass that to our own click handler for use. – Gabriele Petrioli Jan 17 '13 at 16:22
  • does this work like the old JQuery toggle? I guess my question really is, can I use effects with this function? like old: `toggle('slow', function(){...})` – Ares Draguna Jun 15 '15 at 12:50
  • @AresDraguna, no. The normal toggle can do that (*if you include jquery UI*). This is an implementation that an older version of jquery had that was doing a completely different thing. It was used to cycle through a list of functions. (*it had nothing to do with toggling an element on screen, see http://api.jquery.com/toggle-event/*) – Gabriele Petrioli Jun 15 '15 at 13:00