in Jquery would be this:
$('body').toggle(
function(){
alert('A')
}, function(){
alert('B')
}, function(){
alert('C')
}, function(){
alert('D')
}
);
That is, several functions assigned to the body of your document following an execution order.
But you want in pure Javascript, I took advantage and also provided you a choice of order possible, which would be in the case by changing the function parameter toggle()
of functions A
,B
,C
, and D
you could do whatever order you want, or repeat some function without having to include equal functions again.
That 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)");
Thus would execute:
A,B,C,D
It would be the correct circular shape in order, and I’m assigning the click to the tag <body>
of your html so not using DOM.
You can change the order dynamically by changing the code of the past functions.
You can also facilitate your job of adding functions dynamically by placing them on aryFunctions
which would be an Array making your work much easier in this regard.
See, if I change the array functions 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)");
Thus would execute:
A,D,B,C
This way you have enough freedom to do the such toggle()
however you prefer.
Could you specify the goal so that we understand what you really want to do, and what’s your problem? What’s the point of your code?
– Paulo Roberto Rosa
I have an application that does not use jQuery, and there is code function that could very well use this method. It’s been about a week since I’ve been trying to make a hand I can’t, so I’d like the help of someone who understands better...
– Iago Bruno
Do you want to toggle without using jQuery ? ok I’ll edit my answer
– Paulo Roberto Rosa
Just one piece of information, this function has been removed from jQuery since version 1.9
– bfavaretto
I was trying to look at version 1.7.2 (a lot of people like this version so I always look at it rsrsrs)
– Iago Bruno
@Iagobruno I edited my answer including a javascript solution that you can change the order and assign the functions via Array’s facilitating the dynamicity of the code.
– Paulo Roberto Rosa
what would be
arguments[0]
is actually a URL argument?– Paulo Roberto Rosa
could be a code to specify which is the array, if it is D, it will execute one function at a time from array D, if it is 'Hr' it will execute one function at a time from 'Hr', if there is no array 'Hr', it creates a new array with the functions passed to toggle
– Iago Bruno
got it! I’ll edit my answer :)
– Paulo Roberto Rosa
@Iagobruno I did as you said, look at my "aryFunctions" as if it were your "D":) but the part of creating an array if there are no functions would not be part of the function, you would have to check if the array is empty or null and create another one, as you want
– Paulo Roberto Rosa
I think that’s good enough for me, check if it’s function, if it’s empty, it’s no problem
– Iago Bruno