Skip to content

Instantly share code, notes, and snippets.

@gerbenvandijk
Created November 19, 2013 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerbenvandijk/7542958 to your computer and use it in GitHub Desktop.
Save gerbenvandijk/7542958 to your computer and use it in GitHub Desktop.
jQuery function to toggle a click and run custom code on them (.toggle() always shows or hides an element, so we can't use this anymore).
(function($) {
$.fn.clickToggle = function(func1, func2) {
var funcs = [func1, func2];
this.data('toggleclicked', 0);
this.click(function() {
var data = $(this).data();
var tc = data.toggleclicked;
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % 2;
});
return this;
};
}(jQuery));
// usage
$("element").clickToggle(function() {
// action
}, function() {
// action
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment