Reputation: 723
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
}
Upvotes: 1
Views: 3048
Reputation: 23836
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()" />
Upvotes: 2
Reputation: 29683
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()">
Upvotes: 7
Reputation: 388436
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()">
Upvotes: 2
Reputation: 2432
use ondblclick
instead of onclick
Like below
<input type="button" value="Message" ondblclick="showMessage()">
Upvotes: 1