+ 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.

12th Dec 2018, 3:40 PM
silentlearner
silentlearner - avatar
5 Answers
+ 5
You could create a click event function set to a closure Here the example: https://code.sololearn.com/WWXqYfP2FVEK/?ref=app
13th Dec 2018, 1:23 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 4
Thank you for helping me out, everyone. 😊 💕
15th Dec 2018, 5:01 AM
silentlearner
silentlearner - avatar
+ 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
14th Dec 2018, 3:08 PM
777
777 - avatar
+ 2
You could reassign onclick property of button at end of each function to next one.
15th Dec 2018, 1:54 AM
Gray
Gray - avatar
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
15th Dec 2018, 6:56 AM
seyed mohamad hosein abedy nejad
seyed mohamad hosein abedy nejad - avatar