Is there a better way of toggling between different functions, on click events ? Something maybe that use "on" "off" methods or simpler alternative ? Since toggle method has been removed from the newest versions of jQuery, available answers to this problem seem broken,so this question has been asked again.
(function($) {
$.fn.clickToggle = function(func1, func2, func3) {
var funcs = [func1, func2, func3];
this.data('toggleclicked', 0);
this.click(function() {
var data = $(this).data();
var tc = data.toggleclicked;
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % 3;
});
return this;
};
}(jQuery));
$('#b1').clickToggle(
function() {alert('First handler');},
function() {alert('Second handler');},
function() {alert('Third handler');}
);
I am going to answer my own question, which might come handy for someone who is looking for an alternative ways to achieving this with "on" and "off" methods;
$("#b4").on('click', firstClick)
function firstClick() {
alert("First Clicked");
$("#b4").off('click').on('click', secondClick)
}
function secondClick() {
alert("Second Clicked");
$("#b4").off('click').on('click', firstClick)
}