Created
November 19, 2013 09:54
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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