How to make a Button Clickable only once in JavaScript?

To make a button clickable only once, just set it to disable or remove its existing onclick event on the first execution.


<button id="myButton" onclick="myFunction()">Click the button</button>

    function myFunction() {
        document.getElementById("myButton").disabled = true;

This will allow you to print only one “hello” in console, no matter how many times you’ll click the button (it’ll be disabled after the first click).