admin管理员组文章数量:1356873
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
}
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
}
Share
Improve this question
edited Aug 10, 2017 at 3:21
Cœur
38.8k25 gold badges205 silver badges277 bronze badges
asked Sep 21, 2015 at 6:25
DhanaDhana
7233 gold badges14 silver badges41 bronze badges
2
-
Do you want it to
alert
after every first click..? – Rayon Commented Sep 21, 2015 at 6:31 - The question is somewhat ambiguous. You want to track second click/each click after second click/each even click – Tushar Commented Sep 21, 2015 at 6:33
4 Answers
Reset to default 7Use 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()">
本文标签: javascriptHow to get alert message on second click of a buttonStack Overflow
版权声明:本文标题:javascript - How to get alert message on second click of a button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743970826a2570684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论