+ 9
How do you execute a different function each time the same button is clicked in JavaScript?
Each time the user clicks on a button, a different function will be executed. For example, let's say there's empty <p> tags on the html page. First Click: Display a block of paragraph. Second Click: Replace the current paragraph with a new one. Third Click: The new paragraph gets replaced with another different paragraph. Fourth Click: The paragraph starts rotating. Fifth Click: The paragraph disappears. Sixth Click: //next function. Seventh Click: //some other function.
5 Answers
+ 5
You could create a click event function set to a closure
Here the example:
https://code.sololearn.com/WWXqYfP2FVEK/?ref=app
+ 4
Thank you for helping me out, everyone. đ đ
+ 3
You can make an array of functions and then call them when "click" event happens on button:
For example: https://code.sololearn.com/Wy43NnueaE5z/#js
+ 2
You could reassign onclick property of button at end of each function to next one.
0
Hi
Put a variable and add like a = 0
And add 1 to it on each click
Use switch for this varible and run diffrent function for diffrent value of variable a ,
Use onclick event for adding 1 to a