jQuery Toggle Between Functions Fn1 Fn2 Fn3 On Click
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Is there a better way of toggling between different functions, on click events ? Something maybe that use "on" "off" methods or simpler alternative ?
(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');
);
javascript jquery
add a comment |Â
up vote
0
down vote
favorite
Is there a better way of toggling between different functions, on click events ? Something maybe that use "on" "off" methods or simpler alternative ?
(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');
);
javascript jquery
I think this is a good method. I think using.off()
and.on()
to change the handler won't work, because when the next handler is added with.on()
, and the old handler returns, it will then run the next handler immediately.
â Barmar
Jan 12 at 23:08
That will cause an infinite loop.
â Barmar
Jan 12 at 23:09
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is there a better way of toggling between different functions, on click events ? Something maybe that use "on" "off" methods or simpler alternative ?
(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');
);
javascript jquery
Is there a better way of toggling between different functions, on click events ? Something maybe that use "on" "off" methods or simpler alternative ?
(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');
);
javascript jquery
asked Jan 6 at 14:31
skyisfalling
6
6
I think this is a good method. I think using.off()
and.on()
to change the handler won't work, because when the next handler is added with.on()
, and the old handler returns, it will then run the next handler immediately.
â Barmar
Jan 12 at 23:08
That will cause an infinite loop.
â Barmar
Jan 12 at 23:09
add a comment |Â
I think this is a good method. I think using.off()
and.on()
to change the handler won't work, because when the next handler is added with.on()
, and the old handler returns, it will then run the next handler immediately.
â Barmar
Jan 12 at 23:08
That will cause an infinite loop.
â Barmar
Jan 12 at 23:09
I think this is a good method. I think using
.off()
and .on()
to change the handler won't work, because when the next handler is added with .on()
, and the old handler returns, it will then run the next handler immediately.â Barmar
Jan 12 at 23:08
I think this is a good method. I think using
.off()
and .on()
to change the handler won't work, because when the next handler is added with .on()
, and the old handler returns, it will then run the next handler immediately.â Barmar
Jan 12 at 23:08
That will cause an infinite loop.
â Barmar
Jan 12 at 23:09
That will cause an infinite loop.
â Barmar
Jan 12 at 23:09
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
Your general approach seems sound. But you shouldn't hard-code the number of handlers. Take the functions as a rest-arg, and use funcs.length
in the modulo expression.
You can also use the argument to .data()
to look up the named data element, rather than getting the whole data object.
$.fn.clickToggle = function(...funcs)
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % funcs.length;
);
return this;
;
You might also want to special-case a single function and just bind it the normal way, and completely ignore the call if there are no functions.
$.fn.clickToggle = function(...funcs)
switch (funcs.length)
case 0:
break;
case 1:
this.click(funcs[0]);
break;
default:
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
$(this).data("toggleclicked", (tc + 1) % funcs.length);
);
return this;
;
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Your general approach seems sound. But you shouldn't hard-code the number of handlers. Take the functions as a rest-arg, and use funcs.length
in the modulo expression.
You can also use the argument to .data()
to look up the named data element, rather than getting the whole data object.
$.fn.clickToggle = function(...funcs)
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % funcs.length;
);
return this;
;
You might also want to special-case a single function and just bind it the normal way, and completely ignore the call if there are no functions.
$.fn.clickToggle = function(...funcs)
switch (funcs.length)
case 0:
break;
case 1:
this.click(funcs[0]);
break;
default:
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
$(this).data("toggleclicked", (tc + 1) % funcs.length);
);
return this;
;
add a comment |Â
up vote
1
down vote
Your general approach seems sound. But you shouldn't hard-code the number of handlers. Take the functions as a rest-arg, and use funcs.length
in the modulo expression.
You can also use the argument to .data()
to look up the named data element, rather than getting the whole data object.
$.fn.clickToggle = function(...funcs)
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % funcs.length;
);
return this;
;
You might also want to special-case a single function and just bind it the normal way, and completely ignore the call if there are no functions.
$.fn.clickToggle = function(...funcs)
switch (funcs.length)
case 0:
break;
case 1:
this.click(funcs[0]);
break;
default:
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
$(this).data("toggleclicked", (tc + 1) % funcs.length);
);
return this;
;
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Your general approach seems sound. But you shouldn't hard-code the number of handlers. Take the functions as a rest-arg, and use funcs.length
in the modulo expression.
You can also use the argument to .data()
to look up the named data element, rather than getting the whole data object.
$.fn.clickToggle = function(...funcs)
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % funcs.length;
);
return this;
;
You might also want to special-case a single function and just bind it the normal way, and completely ignore the call if there are no functions.
$.fn.clickToggle = function(...funcs)
switch (funcs.length)
case 0:
break;
case 1:
this.click(funcs[0]);
break;
default:
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
$(this).data("toggleclicked", (tc + 1) % funcs.length);
);
return this;
;
Your general approach seems sound. But you shouldn't hard-code the number of handlers. Take the functions as a rest-arg, and use funcs.length
in the modulo expression.
You can also use the argument to .data()
to look up the named data element, rather than getting the whole data object.
$.fn.clickToggle = function(...funcs)
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
data.toggleclicked = (tc + 1) % funcs.length;
);
return this;
;
You might also want to special-case a single function and just bind it the normal way, and completely ignore the call if there are no functions.
$.fn.clickToggle = function(...funcs)
switch (funcs.length)
case 0:
break;
case 1:
this.click(funcs[0]);
break;
default:
this.data('toggleclicked', 0);
this.click(function()
var tc = $(this).data("toggleclicked");
$.proxy(funcs[tc], this)();
$(this).data("toggleclicked", (tc + 1) % funcs.length);
);
return this;
;
answered Jan 12 at 23:21
Barmar
27019
27019
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184445%2fjquery-toggle-between-functions-fn1-fn2-fn3-on-click%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think this is a good method. I think using
.off()
and.on()
to change the handler won't work, because when the next handler is added with.on()
, and the old handler returns, it will then run the next handler immediately.â Barmar
Jan 12 at 23:08
That will cause an infinite loop.
â Barmar
Jan 12 at 23:09