28

jQuery's Toggle Event function has been removed as part of version 1.9.

I was using this function like so:

$('#example').toggle(function() {
    do stuff
}, function() {
    do stuff
});

What would be the best way to reproduce this functionality now Toggle Event has gone?

Alec Rust
  • 10,532
  • 12
  • 48
  • 63

2 Answers2

13

Load the MIGRATE and see the code there

See my post about the same thing

Where has fn.toggle( handler(eventObject), handler(eventObject)...) gone?

I have suggested they rename it to fn.toggler instead of removing it

Here is the code - it is a self-contained jQuery plugin and can be used as is.

jQuery.fn.toggle = function( fn, fn2 ) {
  // Don't mess with animation or css toggles
  if ( !jQuery.isFunction( fn ) || !jQuery.isFunction( fn2 ) ) {
    return oldToggle.apply( this, arguments );
  }
  // migrateWarn("jQuery.fn.toggle(handler, handler...) is deprecated");
  // Save reference to arguments for access in closure
  var args = arguments,
  guid = fn.guid || jQuery.guid++,
  i = 0,
  toggler = function( event ) {
    // Figure out which function to execute
    var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
    jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
    // Make sure that clicks stop
    event.preventDefault();
    // and execute the function
    return args[ lastToggle ].apply( this, arguments ) || false;
  };
  // link all the functions, so any of them can unbind this click handler
  toggler.guid = guid;
  while ( i < args.length ) {
    args[ i++ ].guid = guid;
  }
  return this.click( toggler );
};

Shorter, non-tested version:

(function( $ ){
  $.fn.toggler = function( fn, fn2 ) {
    var args = arguments,guid = fn.guid || $.guid++,i=0,
    toggler = function( event ) {
      var lastToggle = ( $._data( this, "lastToggle" + fn.guid ) || 0 ) % i;
      $._data( this, "lastToggle" + fn.guid, lastToggle + 1 );
      event.preventDefault();
      return args[ lastToggle ].apply( this, arguments ) || false;
    };
    toggler.guid = guid;
    while ( i < args.length ) {
      args[ i++ ].guid = guid;
    }
    return this.click( toggler );
  };
})( jQuery );
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks for that. I already used the [jQuery Migrate](https://github.com/jquery/jquery-migrate/) plugin to determine that this function has been removed (it wasn't documented very well) but I'd rather not leave this plugin in as a solution. What would be a good equivalent? – Alec Rust Jan 15 '13 at 12:49
  • Add the above code to your scripts. it is a plugin in its own right – mplungjan Jan 15 '13 at 12:56
  • 2
    I'm surprised there's not a more concise way of reproducing this functionality. Thanks though! – Alec Rust Jan 15 '13 at 12:59
  • If you rename the function and remove the test for other toggle and comments you have only a few lines of code – mplungjan Jan 15 '13 at 13:38
  • 1
    Fair enough. Marked as answer, thanks again. – Alec Rust Jan 15 '13 at 14:07
8

This also works well.

$.fn.toggleClick = function(){

    var functions = arguments ;

    return this.click(function(){
            var iteration = $(this).data('iteration') || 0;
            functions[iteration].apply(this, arguments);
            iteration = (iteration + 1) % functions.length ;
            $(this).data('iteration', iteration);
    });
};
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Dionysios Arvanitis
  • 1,133
  • 9
  • 11