JavaScript - Disable Button after Click using JavaScript Function

In this code snippet we will learn how to disable a button on click event. In this example there will be button and it will be disabled after click on the button.

Function to Disable Button on Click

JavaScript function:

<script type="text/javascript">
    function disableButton(btn) {
        document.getElementById(btn.id).disabled = true;
        alert("Button has been disabled.");
    }
</script>

HTML Source Code with JavaScript:

<!--JavaScript - Disable Button after Click using JavaScript Function.-->
<html>
    <head>
        <title>JavaScript - Disable Button after Click using JavaScript Function.</title>
        <script type="text/javascript">
            function disableButton(btn) {
                document.getElementById(btn.id).disabled = true;
                alert("Button has been disabled.");
            }
        </script>
    </head>

    <body style="text-align: center;">
        <h1>JavaScript - Disable Button after Click using JavaScript Function.</h1>
        <p><input type="button" id="btn1" value="Click to disable button." onclick="disableButton(this)"</p>
    </body>
</html>

Result:

JavaScript function to disable button

JavaScript Examples »



Related Examples




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.