+ 2
How can I make a button that when clicked on it,it calls a function and if you clickes on the button again it calls another func
18 Antworten
+ 4
You can create an array and add your functions reference to it, later you can call the desired function by using its index. Here is an example of an array filled with 3 function references, the switch statement in the forButton() function decides which function will be called next time forButton() function is called, and since we bound the button's click event to forButton() then the functions call will be switched when one click on the button.
function one(a){
alert("this is from function one " + a);
}
function two(a){
alert("this is from function two " + a);
}
function three(a){
alert("this is from function three " + a);
}
var funcs=[];
var funcIndex = 0;
window.onload=function(){
funcs.push(one);
funcs.push(two);
funcs.push(three);
}
function forButton(argument){
funcs[funcIndex](argument);
switch(funcIndex)
{
case 0:
funcIndex = 1;
break;
case 1:
funcIndex = 2;
break;
case 2:
funcIndex = 0;
break;
}
}
And a button for triggering forButton();
<button onclick="forButton('SoloLearn');">Click Me</button>
Hth, cmiiw
+ 1
Sorry I have a question of you
0
Can you make an example
0
Oh thanks
0
Why did you write one=function...
Why you did not write function one()
0
and what is this "if(!clicked)" "!"
0
Plz see this
0
I made it myself
0
ot works like your code
0
Thank you so much
0
Your welcome
0
Oh thank you so much but I think Your level is higher than me so I respect to you
0
👍👍
0
Why it doesn't work correctly
0
nothing
0
Thank you