Javascript call function first second

I want to make it so that the first click I call one function for javascript, and in the second click I call another function for javascript. How can it be?

Basically I click on this image, it calls this function, if I click it again, it calls another function. First second.

Using html

+4
source share
5 answers
<script> var clicked = false; function doSomething() { if(clicked) { alert(2); } else { alert(1); } clicked = !clicked; } </script> </head> <body> <p id="hello" onClick="doSomething();">Hello World</p> </body> 

See here http://jsbin.com/uzivuj

+12
source

First of all, you mean JavaScript.

Basically, in the first event handler, you want to delete it and then assign a new one:

 function foo() { // Other work this.removeEventListner("click", foo, false); this.addEventListener("click", bar, false); } function bar() { } element.addEventListener("click", foo, false); 
+2
source

This will alternate between calls to first() and second() each time the button is pressed.

 <input type="button" id="mybutton" value="Click me" /> <script> function first() { alert("first"); } function second() { alert("second"); } var toggleFlag = true; $("#mybutton").click(function() { toggleFlag ? first() : second(); toggleFlag = !toggleFlag; }); </script> 
0
source

Hope this help helps you.

 <a href=# onclick="onClickEvent()"> <img src="[Src]" width="208" height="58" > </a> var count = 0; function function1(){ alert("Function 1"); count++; } function function2(){ alert("Function 2"); count = 0; } function onClickEvent(){ if(count ==0){ function1(); }else{ function2(); } } 
0
source

try it

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#mybutton").click(function(){ $("p").toggle(); });`enter code here` }); </script> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <input type="button" id="mybutton" value="Click me to show" /> 
-2
source

Source: https://habr.com/ru/post/1382824/


All Articles