Table of contents
1.
Introduction
2.
Definition & Usage
3.
Syntax
3.1.
Example
4.
HTML Button Disabled Attribute Example
5.
HTML Button Disabled Attribute Use Cases
5.1.
1. Preventing Form Submission Before Input
5.2.
2. Preventing Multiple Clicks
5.3.
3. Showing Different States
6.
Supported Browsers
7.
Frequently Asked Questions
7.1.
Can we enable a disabled button dynamically using JavaScript?
7.2.
Does the disabled button send data in a form submission?
7.3.
Can CSS style a disabled button differently?
8.
Conclusion
Last Updated: Feb 23, 2025
Easy

HTML <button> disabled Attribute

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The disabled attribute in the HTML <button> tag is used to disable a button, preventing user interactions such as clicking. When this attribute is present, the button appears grayed out and cannot trigger any event. It is commonly used in forms to restrict actions until specific conditions are met. 

HTML <button> disabled Attribute

In this article, you will learn about the syntax, functionality, and examples of the disabled attribute in the <button> tag.

Definition & Usage

The "disabled" attribute in HTML is used to make a button non-clickable or inactive. When a button is disabled, users cannot interact with it, & it appears grayed out in most browsers. This feature is useful when you want to prevent users from clicking a button until certain conditions are met, such as filling out a form or agreeing to terms & conditions.

The "disabled" attribute can be added directly to the `<button>` tag. It does not require a value; simply including the word "disabled" inside the tag will deactivate the button. For example: 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Disabled Button Example</title>
</head>
<body>
    <h1>Example of a Disabled Button</h1>
    <form>
        <label for="username">Enter your username:</label>
        <input type="text" id="username" name="username"><br><br>

        <button type="submit" disabled>Submit</button>
    </form>
</body>
</html>

 

Output

Output

In this example:

  • The `<button>` tag includes the "disabled" attribute.
     
  • Since the button is disabled, users cannot click it to submit the form.
     
  • The purpose here could be to ensure that the user fills out the input field before enabling the button.
     

You can also enable or disable buttons dynamically using JavaScript. For instance, you can disable a button initially & then enable it once the user completes a required action. For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enable Button Dynamically</title>
    <script>
        function enableButton() {
            document.getElementById("submitButton").disabled = false;
        }
    </script>
</head>
<body>
    <h1>Enable Button After Input</h1>
    <form>
        <label for="password">Enter your password:</label>
        <input type="password" id="password" name="password" oninput="enableButton()"><br><br>

        <button type="submit" id="submitButton" disabled>Submit</button>
    </form>
</body>
</html>

 

Output

Output

 

In this code:

  • The button starts off disabled.
     
  • As soon as the user types something into the password field, the `oninput` event triggers the `enableButton()` function.
     
  • This function removes the "disabled" attribute from the button, making it clickable.
     

The "disabled" attribute is commonly used in forms, modals, & other interactive elements to guide users through a process or prevent accidental actions. It ensures that users follow the intended flow of the application.

Syntax

The disabled attribute is a boolean attribute, meaning that if it is present, the button will be disabled. It does not require a value. Here is the syntax:

<button disabled>Click Me</button>

Example

<!DOCTYPE html>
<html>
<head>
    <title>Disabled Button Example</title>
</head>
<body>
    <button disabled>Submit</button>
</body>
</html>

 

Output

Output

A button labeled "Submit" will appear on the webpage, but it will be grayed out and unclickable.

HTML Button Disabled Attribute Example

The disabled attribute is often used in forms where users need to provide information before submitting. Let’s see an example where the button is disabled until the user enters text into an input field.

<!DOCTYPE html>
<html>
<head>
    <title>Enable Button Example</title>
    <script>
        function enableButton() {
            let inputField = document.getElementById("userInput");
            let submitButton = document.getElementById("submitBtn");
            
            if (inputField.value.trim() !== "") {
                submitButton.disabled = false;
            } else {
                submitButton.disabled = true;
            }
        }
    </script>
</head>
<body>
    <label for="userInput">Enter your name:</label>
    <input type="text" id="userInput" onkeyup="enableButton()">
    <button id="submitBtn" disabled>Submit</button>
</body>
</html>


Output

Output

Explanation

  • The button is initially disabled.
     
  • When the user starts typing, the enableButton() function checks if the input field is empty or not.
     
  • If the input field contains text, the button becomes enabled.

HTML Button Disabled Attribute Use Cases

The disabled attribute is widely used in web development. Some common use cases include:

1. Preventing Form Submission Before Input

Ensures users fill out necessary details before submitting a form.

<form>
    <input type="email" id="emailInput" onkeyup="checkEmail()">
    <button id="submitButton" disabled>Submit</button>
</form>

<script>
    function checkEmail() {
        let email = document.getElementById("emailInput").value;
        document.getElementById("submitButton").disabled = email === "";
    }
</script>

2. Preventing Multiple Clicks

Disables the button after one click to prevent duplicate submissions.

<button id="saveButton" onclick="disableButton()">Save</button>

<script>
    function disableButton() {
        document.getElementById("saveButton").disabled = true;
    }
</script>

3. Showing Different States

Can indicate different application states, such as "Processing" or "Loading".

<button id="processButton" disabled>Processing...</button>

Supported Browsers

The disabled attribute is widely supported by all major browsers. Below is a compatibility table:

BrowserSupport
Google Chrome Yes
Mozilla Firefox Yes
Microsoft Edge Yes
Safari Yes
Opera Yes

All modern browsers fully support the disabled attribute for buttons.

Frequently Asked Questions

Can we enable a disabled button dynamically using JavaScript?

Yes, you can use JavaScript to enable a button by setting disabled = false.

document.getElementById("myButton").disabled = false;

Does the disabled button send data in a form submission?

No, a disabled button will not trigger form submission or send data.

Can CSS style a disabled button differently?

Yes, you can use CSS to change the appearance of disabled buttons.

Conclusion

In this article, we learned the disabled attribute of the <button> tag in HTML, which prevents users from interacting with the button. A disabled button appears grayed out and does not trigger any actions. This attribute is useful for form validation and controlling user actions. Understanding the disabled attribute helps in improving user experience and accessibility in web development.

Live masterclass