how to disable a button in javascript based on condition?

Rate this post

how to disable a button in javascript based on condition – Enable or Disable Button based on condition using JavaScript Example with full source code.

Free Live Chat for Any Issue

how to disable a button in javascript based on condition

Set button disable property false for disabled it and true for enabled it in JavaScript.

use the disabled attribute

<button type="submit" class="checkout-button" id="checkout-button" name="checkout-button" disabled="disabled"></button>

How to disable a button in JavaScript based on condition

index.html

<html>
<body>
    <input type="text" id="txt" onkeyup="EnableDisable(this)" />

    <input type="submit" id="loginButton" disabled value="Submit" />
</body>
<script>
    function EnableDisable(txt) {
        var loginButton = document.getElementById('loginButton');
        if (txt.value != '') {
            loginButton.disabled = false;
        }
        else {
            loginButton.disabled = true;
        }
    }
</script>
</html>

How to enable/disable an html button based on scenarios?

Enable or Disable Button based on condition using JavaScript – Enable the TextBox when TextBox has value and Disable the TextBox when TextBox is empty.

GSTIN Number:
<input type="text" id="txtGSTINNumber" onkeyup="EnableDisable(this)" />
<hr />
<input id="loginButton" type="button" value="Submit" disabled="disabled" />
<script type="text/javascript">
    function EnableDisable(txtGSTINNumber) {

        var loginButton = document.getElementById("loginButton");
 
        if (txtGSTINNumber.value.trim() != "") {
            loginButton.disabled = false;
        } else {
            loginButton.disabled = true;
        }
    };
</script>

Download

I hope you get an idea about how to disable a button in javascript based on condition?.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.