简体   繁体   中英

Show alert when a button click for second time in javaScript

I have a button and I want to show an alert when user click on the button for second time. Actually I want to disable second click on a button and show error alert !

How can I do this with JavaScript ?

Having this button in your HTML

<button onclick="whichClick()">Click me</button>

You can add a class on your first click and then check is the class is already present, so you can throw the error:

function whichClick(e) {
    if (e.target.className.contains('clicked')) {
       alert("error");
    } else {
       e.target.className.addClass('clicked');
    }
}

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