In JQuery this would be it:
$('body').toggle(
function(){
alert('A')
}, function(){
alert('B')
}, function(){
alert('C')
}, function(){
alert('D')
}
);
That is, various functions assigned to the body of your document following an order of execution.
But you want it in pure Javascript, I took advantage of it and also gave you a choice of order , which would in this case change the function parameter toggle()
of functions A
, B
, C
, and D
you could do any order you want, or repeat some function without having to include equal functions again.
This would be the toggle function:
function toggle(aryFunctions,idx){
document.getElementsByTagName("body")[0].onclick = aryFunctions[idx];
}
And to use we declare an array of functions:
function A(){
alert('A');
toggle(aryFunctions,1);
}
function B(){
alert('B');
toggle(aryFunctions,2);
}
function C(){
alert('C');
toggle(aryFunctions,3);
}
function D(){
alert('D');
toggle(aryFunctions,0);
}
var aryFunctions = [A,B,C,D];
document.body.setAttribute("onclick","toggle(aryFunctions,0)");
This would execute:
A,B,C,D
It would be the correct way to circulate in order, and I'm assigning the click to the <body>
tag of your html so not using DOM.
You can change the order dynamically by changing the code of the past functions.
You can also make it easier for you to add functions dynamically by putting them in aryFunctions
which would be an Array, making your work a lot easier in this respect.
See if I change the functions of the array to:
function A(){
alert('A');
toggle(aryFunctions,3);
}
function B(){
alert('B');
toggle(aryFunctions,2);
}
function C(){
alert('C');
toggle(aryFunctions,0);
}
function D(){
alert('D');
toggle(aryFunctions,1);
}
var aryFunctions = [A,B,C,D];
document.body.setAttribute("onclick","toggle(aryFunctions,0)");
This would execute:
A,D,B,C
In this way you have enough freedom to make this toggle()
as you prefer.