How to Disable a Button Conditionally in JavaScript? Last Updated : 20 May, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, you can conditionally disable buttons based on specific criteria to control user interactions. Using simple if-else statements or event listeners, you can dynamically enable or disable buttons depending on form validation, user actions, or other conditions. This helps regulate the availability of buttons, ensuring they are only active when appropriate. ApproachThe HTML form includes text and password input fields and a submit button that is initially disabled.CSS styles are applied to the button and input fields for better visual appearance, with specific dimensions and colors.JavaScript code runs after the DOM is fully loaded, targeting the form and submit button.An event listener on the form detects input changes and checks form validity.If the form is valid, the submit button is enabled and its background color changes to green; otherwise, the button remains disabled. Similarly, in the second example if the checkbox is checked, enable the button and add the .green class otherwise keep it disabled.Example 1: The example below Check the form validation status before enabling the submit button. If validation passes, enable the button with a green background otherwise keep it disabled. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Conditional Button Disable</title> <link rel="stylesheet" href="styles.css"> </head> <body> <form id="myForm"> <input type="text" id="username" placeholder="Username" required><br> <input type="password" id="password" placeholder="Password" required><br> <button type="submit" id="submitButton" disabled> Submit </button> </form> <script src="script.js"></script> </body> </html> CSS #submitButton { margin: 10px; padding: 10px; color: white; font-size: large; } #username, #password { width: 200px; height: 30px; border: 2px solid green; margin: 10px; } #myForm { display: flex; flex-direction: column; align-items: center; } JavaScript document.addEventListener('DOMContentLoaded', function () { var form = document.getElementById('myForm'); var submitButton = document.getElementById('submitButton'); form.addEventListener('input', function () { if (form.checkValidity()) { submitButton.disabled = false; submitButton.style.backgroundColor = "green"; } else { submitButton.disabled = true; } }); }); Output: OutputExample 2: The example below check if the checkbox is checked before enabling the button. If checked, enable the button and add the .green class otherwise keep it disabled. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Conditional Button Disable</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="container"> <div class="checkbox"> <input type="checkbox" id="terms"> <label for="terms"> I agree to the terms and conditions </label> </div> <button id="actionButton" disabled> Perform Action </button> </div> <script src="script.js"></script> </body> </html> CSS .container { display: flex; align-items: center; flex-direction: column; margin: 100px; } .checkbox { margin: 10px; padding: 20px; color: green; font-size: large; } #actionButton { margin: 10px; padding: 10px; color: rgb(15, 1, 1); font-size: large; } .green { background-color: green; } JavaScript const termsCheckbox = document.getElementById('terms'); const actionButton = document.getElementById('actionButton'); termsCheckbox.addEventListener('change', validateCheckbox); function validateCheckbox() { actionButton.disabled = !termsCheckbox.checked; if (termsCheckbox.checked) { actionButton.classList.add('green'); } else { actionButton.classList.remove('green'); } } actionButton.addEventListener('click', function () { document.getElementById('terms').checked = false; actionButton.disabled = true; actionButton.classList.remove('green'); }); Output: Output Comment More infoAdvertise with us Next Article How to Disable a Button Conditionally in JavaScript? S studynoxv5b Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Disable a Button in React JS ? Disabling a button in React JS means making it unclickable and visually indicating its unavailability. This is done by setting the button's disabled attribute to true. It's used for controlled interactions, like form submissions with valid data.A disabled button becomes unclickable and visually sign 4 min read JavaScript- Disable and Enable Button in JS These are the following approaches to Disable and enable a Button in JavaScript:1. Using the disabled Attribute The code example toggles the 'myButton' state between enabled and disabled using the 'toggleButton'. Clicking the 'toggleButton' triggers the 'toggleButtonState' function, which checks if 2 min read How to Disable a Button in jQuery UI ? To disable a button in jQuery UI, we will be using disable() method which is discussed below: jQuery UI disable() method is used to completely disable the button. It returns the button element completely to its initial state. Syntax: $(".selector").button("disable") Parameters: This method does not 2 min read How to disable radio button using JavaScript ? Radio buttons let users select one option from many, commonly used in forms. Sometimes, based on a user's previous choice, certain options need to be disabled. For example, if a user selects "No" to playing games, we can disable related game options. Using JavaScript, about 65% of forms use such log 4 min read How to Disable Input in JavaScript ? Disabling input fields in JavaScript is a common task when you want to restrict users from interacting with specific form elements. This can be useful in various scenarios, such as preventing modifications to fields that are conditionally locked or ensuring certain inputs are controlled programmatic 2 min read How to Disable Submit Button on Form Submit in JavaScript ? Forms are a crucial part of web development as they allow users to submit data to a server. However, sometimes we want to customize the behavior of HTML forms and prevent the default behavior from occurring. This allows you to create a more interactive and user-friendly web application. In this arti 3 min read How to Disable Button Element Dynamically using JavaScript? The disabled property in the HTML DOM is used to control the interactivity of a button element. When this property is set, the button becomes unclickable and inactive, effectively preventing any user interaction. This property accepts a boolean value, allowing developers to dynamically enable or dis 1 min read How to disable a button in jQuery dialog from a function ? In this article, we will learn to disable a button in the jQuery dialog from a function. The basic dialog window is an overlay positioned within the viewport and is protected from page content which is jQuery's User Interface. To disable the button in a jQuery dialog from a function carried out usin 3 min read How to Disable Radio Button in HTML? This article will show you how to disable Radio Button in HTML. The disabled attribute is used to disable a radio button in HTML. This attribute prevents the radio button from being selected by the user. Using the disabled AttributeThe disabled attribute is used to disable the input fields. To disab 2 min read How to disable a button depending on a checkboxâs state in AngularJS ? In this article, we will learn to disable the button depending upon the check-box status in Angular. We will use the Angular JS directive named ng-disabled to disable the button by unchecking the box. Please refer to AngularJS ng-disabled Directive. The ng-disabled directive is used to enable or dis 2 min read Like