Programming

How to Disable a Button in JavaScript (HTML, CSS, jQuery and React Examples)

A WooCommerce store we maintain started getting duplicate orders every week. Customers on slow mobile connections tapped Place Order, saw nothing happen and tapped again, so the payment gateway charged them twice. The fix was four lines of JavaScript that disabled the button the moment it was clicked and showed "Processing..." instead. The duplicate orders stopped that day.

Front-end developer disabling a checkout button in JavaScript, with code on one screen and a greyed-out button on the other

That’s the most common reason to disable a button in JavaScript, but not the only one. You might want a submit button disabled until a form is valid, a Next button disabled until a user agrees to terms or a button disabled while data loads. Below we show how to disable a button in JavaScript with the disabled property, how to disable it based on a condition, how to enable it again and how to do the same in HTML, CSS, jQuery and React. We also cover accessibility and a Chrome back button issue that catches many developers out.

Quick answer: To disable a button in JavaScript, select it and set its disabled property to true, for example document.getElementById(“myBtn”).disabled = true. Set it back to false to enable the button. In HTML, add the disabled attribute to the button tag. In React, pass disabled={condition}, and in jQuery use $(“#myBtn”).prop(“disabled”, true).

How to Disable a Button in JavaScript

The simplest way to disable a button in JavaScript is to set the button’s disabled property to true. A disabled button can’t be clicked, can’t receive keyboard focus and isn’t submitted with a form. Browsers also show it greyed out by default, so users can see it’s unavailable.

Using the disabled Property

const button = document.getElementById("submitBtn");

// Disable the button
button.disabled = true;

// Enable the button again
button.disabled = false;

The disabled property maps to the HTML disabled attribute. MDN’s disabled attribute reference lists every element that supports it.

Other Ways to Toggle It

MethodCodeWhen to use it
disabled propertybutton.disabled = trueMost cases, simplest and clearest
setAttributebutton.setAttribute(“disabled”, “”)When working with attributes directly
removeAttributebutton.removeAttribute(“disabled”)To enable a button
toggleAttributebutton.toggleAttribute(“disabled”, isOff)When a boolean decides the state
Ways to disable a button in JavaScript

Avoid setAttribute(“disabled”, “false”). Any value, even “false”, keeps the button disabled, because the attribute only needs to exist.

How to Disable a Button in JavaScript Based on a Condition

To disable a button based on a condition, set its disabled property to the result of that condition. Run the check whenever the condition might change, such as when a user types, ticks a checkbox or selects an option. This keeps the button in sync with your form or app state.

Disable Until an Input Has Text

const input = document.getElementById("email");
const button = document.getElementById("submitBtn");

input.addEventListener("input", () => {
  button.disabled = input.value.trim() === "";
});

Disable Until a Checkbox Is Ticked

const terms = document.getElementById("terms");
const nextBtn = document.getElementById("nextBtn");

nextBtn.disabled = !terms.checked;

terms.addEventListener("change", () => {
  nextBtn.disabled = !terms.checked;
});

Disable Until a Form Is Valid

Use the browser’s built-in validation with checkValidity, so the button stays disabled until every required field is filled in correctly.

const form = document.getElementById("signupForm");
const submitBtn = form.querySelector("button[type=submit]");

form.addEventListener("input", () => {
  submitBtn.disabled = !form.checkValidity();
});

Disable a Button in JavaScript After One Click

Disabling a button after one click prevents double submissions, duplicate payments and repeated API calls. Disable it inside the click or submit handler, show a loading message and re-enable it if the request fails, so the user can try again without reloading the page.

Prevent Double Form Submission

const form = document.getElementById("checkoutForm");
const placeOrder = document.getElementById("placeOrder");

form.addEventListener("submit", () => {
  placeOrder.disabled = true;
  placeOrder.textContent = "Processing...";
});

Disable While Fetching Data

const saveBtn = document.getElementById("saveBtn");

saveBtn.addEventListener("click", async () => {
  saveBtn.disabled = true;
  try {
    await fetch("/api/save", { method: "POST" });
  } catch (error) {
    alert("Saving failed. Please try again.");
  } finally {
    saveBtn.disabled = false;
  }
});

The finally block re-enables the button whether the request succeeds or fails, so users never get stuck with a dead button.

How to Enable a Button in JavaScript

To enable a button in JavaScript, set its disabled property to false or remove the disabled attribute. Both work the same way. Enable buttons as soon as the condition that disabled them changes, so users aren’t left wondering why they can’t continue.

Enable Button Examples

GoalCode
Enable with the propertybutton.disabled = false
Enable by removing the attributebutton.removeAttribute(“disabled”)
Enable after a delaysetTimeout(() => button.disabled = false, 3000)
Enable all buttons in a formform.querySelectorAll(“button”).forEach(b => b.disabled = false)
How to enable a button in JavaScript

Show Why a Button Is Disabled

Add short helper text near disabled buttons, such as “Enter your email to continue.” Users then know exactly what to do, which reduces form abandonment.

How to Disable a Button in HTML

To disable a button in HTML, add the disabled attribute to the button element. It’s a boolean attribute, so you don’t need a value. The button loads disabled, and you can enable it later with JavaScript. This is useful for forms that start incomplete.

HTML Examples

<!-- Disabled when the page loads -->
<button type="submit" disabled>Submit</button>

<!-- Disabled inputs and fieldsets work the same way -->
<fieldset disabled>
  <input type="text" name="name">
  <button type="button">Save</button>
</fieldset>

Disable a Button in HTML Based on a Condition

Plain HTML can’t react to conditions by itself. Combine the disabled attribute with a few lines of JavaScript, as shown above, or let your server render the attribute only when the condition is true, such as when an item is out of stock.

How to Disable a Button in CSS

CSS can’t truly disable a button, because disabling is behavior, not style. You can make a button look disabled and block mouse clicks with pointer-events: none, but keyboard users can still tab to it and press Enter. Use CSS to style buttons that are already disabled with the disabled attribute.

Style Disabled Buttons

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-looks-disabled {
  pointer-events: none;
  opacity: 0.5;
}

CSS vs the disabled Attribute

Featuredisabled attributeCSS pointer-events: none
Blocks mouse clicksYesYes
Blocks keyboard activationYesNo
Removes from tab orderYesNo
Excluded from form submissionYesNo
Screen readers announce itYes, as unavailableNo
Disabling a button with HTML vs CSS

For more CSS button and link styling, see our guide on how to remove the underline from links in CSS.

Disable a Button in jQuery

In jQuery, disable a button with the prop method: $(“#myBtn”).prop(“disabled”, true). Set it to false to enable the button again. Use prop rather than attr for disabled, because prop reads and sets the live state of the element, which is what the browser actually uses.

jQuery Examples

// Disable
$("#submitBtn").prop("disabled", true);

// Enable
$("#submitBtn").prop("disabled", false);

// Disable based on a condition
$("#email").on("input", function () {
  $("#submitBtn").prop("disabled", $(this).val().trim() === "");
});

The jQuery .prop() documentation explains why prop is preferred over attr for boolean properties like disabled and checked.

Do You Still Need jQuery?

Most modern projects don’t need jQuery just to disable buttons, since plain JavaScript does it in one line. jQuery is still common in WordPress themes and older sites, so the syntax is worth knowing.

How to Disable a Button in React

In React, disable a button by passing a boolean to the disabled prop, such as disabled={isLoading}. React updates the button automatically whenever that value changes. Keep the condition in state or derive it from form values, rather than changing the DOM directly with JavaScript.

React Example

import { useState } from "react";

export default function SignupForm() {
  const [email, setEmail] = useState("");
  const [loading, setLoading] = useState(false);

  async function handleSubmit(e) {
    e.preventDefault();
    setLoading(true);
    await fetch("/api/signup", { method: "POST", body: email });
    setLoading(false);
  }

  return (
    <form onSubmit={handleSubmit}>
      <input value={email} onChange={(e) => setEmail(e.target.value)} />
      <button type="submit" disabled={!email || loading}>
        {loading ? "Signing up..." : "Sign up"}
      </button>
    </form>
  );
}

Learn More About React

React’s common component props page covers disabled and other attributes. If you’re starting out, read our guides on whether React is a programming language and how to create a React app.

Accessible Disabled Buttons

Disabled buttons can confuse users who rely on keyboards or screen readers, because a disabled button can’t receive focus, so they may never learn why it’s unavailable. For important actions, consider aria-disabled=”true” instead, which keeps the button focusable while telling assistive technology it’s unavailable.

disabled vs aria-disabled

Featuredisabledaria-disabled=”true”
Keyboard focusNot focusableStill focusable
Click eventsBlocked by the browserYou must block them in JavaScript
Screen readerOften skippedAnnounced as unavailable
Tooltips and help textHard to reachEasy to reach
disabled vs aria-disabled for accessibility

MDN’s aria-disabled guide explains when to use each.

Accessibility Tips

Keep enough color contrast so disabled text is still readable, explain why the button is disabled and avoid disabling buttons when showing a clear error message on click would help users more.

JavaScript Disable Button Issues in Chrome

Most disable button problems in Chrome come from caching and event handling, not from Chrome itself. A common one is a button that stays disabled after a user presses Back, because Chrome’s back-forward cache restores the page exactly as it was, including the disabled state you set before submitting.

Common Problems and Fixes

ProblemCauseFix
Button stays disabled after pressing BackBack-forward cache restores the old stateRe-enable it in a pageshow event listener
Click event doesn’t fire on a disabled buttonDisabled buttons don’t fire mouse eventsUse aria-disabled or wrap the button
Tooltip doesn’t show on hoverDisabled elements ignore pointer events in some browsersPut the tooltip on a wrapper element
Button disabled before the form submitsDisabled buttons aren’t submittedDisable it on submit, not on click
Form field values restored oddlyBrowser form restorationAdd autocomplete=”off” where needed
JavaScript disable button problems in Chrome and fixes

Fix the Back Button Issue

window.addEventListener("pageshow", (event) => {
  if (event.persisted) {
    document.getElementById("placeOrder").disabled = false;
  }
});

web.dev’s back-forward cache guide explains how pages are restored from the cache. For other front-end behavior fixes, see how to hide a navbar on scroll in JavaScript.

FAQs

Here are answers to common questions about disabling buttons in JavaScript.

How do I disable a button in JavaScript?

Select the button with document.getElementById or querySelector and set its disabled property to true, for example button.disabled = true. The button turns grey, can’t be clicked and can’t receive keyboard focus. Set the property back to false to enable it again. You can also use setAttribute(“disabled”, “”) and removeAttribute(“disabled”) if you prefer working with attributes. It works in every modern browser.

How do I disable a button in JavaScript based on a condition?

Set the disabled property to the condition itself, for example button.disabled = input.value === “”. Put that line inside an event listener on the field, such as input or change, so it runs every time the condition might change. For forms, button.disabled = !form.checkValidity() keeps the button disabled until all required fields are valid and correctly filled in. Re-check it on every change.

How do I enable a button in JavaScript?

Set the button’s disabled property to false, or remove the disabled attribute with removeAttribute(“disabled”). Both make the button clickable and focusable again. Don’t use setAttribute(“disabled”, “false”), because the attribute’s presence alone disables the button, whatever its value. Enable buttons as soon as the reason for disabling them is gone, so users aren’t left stuck or confused.

How do I disable a button in HTML?

Add the disabled attribute to the button tag, for example button type=”submit” disabled. It’s a boolean attribute, so it doesn’t need a value. The button loads disabled and stays that way until JavaScript removes the attribute. You can disable a whole group of inputs and buttons at once by adding disabled to a fieldset element. Removing it from the fieldset enables them all again.

Can you disable a button with CSS only?

Not fully. CSS can make a button look disabled with opacity and stop mouse clicks with pointer-events: none, but keyboard users can still tab to the button and activate it, and it’s still submitted with forms. Use the HTML disabled attribute for real disabling, then style it with the :disabled CSS selector to match your design. Then add a short label explaining why.

How do I disable a button in jQuery?

Use the prop method: $(“#myBtn”).prop(“disabled”, true) to disable it and $(“#myBtn”).prop(“disabled”, false) to enable it. Use prop instead of attr, because prop changes the live disabled state that the browser uses. You can combine it with event handlers like on(“input”) to disable or enable the button based on a condition. It also reads more clearly in your code.

How do I disable a button in React?

Pass a boolean to the disabled prop, for example button disabled={isLoading}. Store the condition in state with useState or calculate it from your form values, and React re-renders the button whenever the value changes. Avoid changing the button’s DOM directly with document.getElementById inside React components, because React may overwrite your changes on the next render and cause bugs.

How do I disable a button after one click?

Inside the click or submit handler, set button.disabled = true and optionally change the text to something like “Processing…”. If the action uses fetch, re-enable the button in a finally block so it works again if the request fails. Disabling on the form’s submit event, rather than the button’s click, makes sure the form still submits correctly. It’s the simplest fix for double payments.

Why does my button stay disabled when I press Back in Chrome?

Chrome’s back-forward cache restores the page exactly as you left it, including a button you disabled before submitting. Listen for the pageshow event and re-enable the button when event.persisted is true. This simple fix keeps the button usable if a customer goes back to change their order or details before submitting the form again. Test it on mobile browsers too.

Conclusion

To disable a button in JavaScript, set its disabled property to true, and set it to false to enable it again. Tie the property to a condition with event listeners, disable buttons on submit to stop double clicks and re-enable them in a finally block after requests. In HTML use the disabled attribute, in jQuery use prop and in React pass a boolean to the disabled prop. CSS only changes how a button looks, so pair it with the real attribute.

Our client’s duplicate orders stopped the day we disabled the Place Order button on submit, and the pageshow fix kept it usable when customers pressed Back. Think about accessibility too, using aria-disabled and helper text when users need to understand why a button is unavailable. Test every disabled state on mobile, with the keyboard and after pressing Back. Which button on your site would you disable first to prevent mistakes?

Leave a Reply

Your email address will not be published. Required fields are marked *

Share the article

Written By

Author Avatar

November 17, 2026

Hi there! I’m Ayesha Khan, a skilled content writer with a strong background in computer science. I specialize in transforming complex ideas into clear, engaging, and easy-to-understand content. With 10 years of experience working across different industries, I focus on delivering content that not only informs but also connects with readers. I’m passionate about writing and take pride in creating high-quality work that helps clients communicate their message effectively.