Skip to content
Advertisement

A function that runs on the second click

I’m running a function that shows a left menu when I click a button.

I need that the menuColapsado() function to run on the first click of the ID menu_button but the function shows the html element on the second click instead of the first. My code is below

JavaScript
JavaScript

Advertisement

Answer

So, from what I understand, you have a button and you want to run a function the first time it is clicked and another function the second time it is clicked.

Here is a simple solution with a counter and an If statement:

JavaScript

The above code will run the second function for every other time the button is clicked. You can easily change it to suit your needs if you do not want this to happen.

If you want to use every 3rd, 5th, 7th etc click as a first click and every 4th, 6th, 8th etc click as a second click, you can change the If statement and use modulo division:

JavaScript

Check modulo division: How can I use modulo operator (%) in JavaScript?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement