简体   繁体   中英

How to get alert message on second click of a button

I would like to know that how can I get an alert message on second click of a button, not on a first click.

ex: <input type="button" value="Message" onclick="showMessage()">

function showMessage(){
    alert("It's a second click message"); // this alert message should be shown on second click of a button
}

Use counter instead..

 var count = 0; //global variable function showMessage() { if (count++ == 1) { //Compare and then increment counter alert("It's a second click message"); //this alert message will be shown only on second click of a button } } 
 <input type="button" value="Message" onclick="showMessage()"> 

If you are not looking for the dbclick event, then you can use a flag to check whether it is the second click to show the alert

 var flag; function showMessage() { if (flag) { alert("It's a second click message"); // this alert message should be shown on second click of a button } else { flag = true; } } 
 <input type="button" value="Message" onclick="showMessage()"> 

Consider this code, This is alert message after every alternate click:

 var count = 1; function showMessage() { if (!(count++ % 2)) alert("It's a second click message"); //this alert message at every second click } 
 <input type="button" value="Message" onclick="showMessage()" /> 

use ondblclick instead of onclick

Like below

<input type="button" value="Message" ondblclick="showMessage()">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM