Easily Disable a Button with JavaScript or CSS (3 Examples)

Here are two simple ways to disable an HTML button:

  • Set the button’s disabled attribute to true with JavaScript
  • Set pointer-events: none; with CSS

We should also lighten the button’s opacity by about 33% (that’s what Bootstrap disabled class does). In this post, I will show full JavaScript and CSS code for disabling HTML buttons.

Disable Buttons with JavaScript and CSS
Disable Buttons with JavaScript and CSS

Finally, make sure the button cannot be tabbed into and that the button is disabled for screen readers. We’ll see how to do that below.

The Resources section has a live Code Sandbox demo link.

In these examples I applied the following styling to my button elements:

button {
  font-size: 18px;
  border-radius: 4px;
  background-color: #1e73be;
  color: white;
  padding: 6px 12px;
  cursor: pointer;
  border: 4px solid #1e80be;
  width: 200px;

Simple JavaScript Code for Disabling an HTML Button

Here are the steps for disabling a button with JavaScript:

  • Select the button using document.getElementById, document.querySelector, or your preferred method
  • Attach a click event listener
  • Set the disabled attribute to false in the click handler (this requires a reference to the button)

It is important to pass a reference to the button into the click handler. Take a look at the below code to see how I accomplished this:

//HTML Button
<button id="jsDisableButton" type="button" style="margin: 12px;">
  JS Toggle Disable

//JS Click Event Listener
const jsDisableButton = document.getElementById("jsDisableButton");
if (jsDisableButton) {
  jsDisableButton.addEventListener("click", () =>

//JS Click Handler
const handleJSBtnClick = (jsDisableButton) => {
  jsDisableButton.disabled = true;
  console.log("JS Disabled");

This code gets the job done, but I recommend some basic styling on the button when it is disabled:

button:disabled {
  pointer-events: none; //better than cursor: default;
  opacity: 0.65;

The above selector queries for the disabled pseudo class on button elements.

This styling is important because the opacity gives visual indication that the button is unclickable. Removing pointer events keeps hover and click events from occurring. If we only set cursor: default; then the cursor looks inactive but hover and click events can still occur.

You also have the option of passing the click event to the handler. Here’s what the updated code looks like (with TypeScript):

const jsDisableButton = document.getElementById(
) as HTMLButtonElement;
if (jsDisableButton) {
  jsDisableButton.addEventListener("click", (event: MouseEvent) =>
    handleJSBtnClick(event, jsDisableButton)

CSS Styling for Disabling an HTML Button

We can set a couple of style properties on a button to effectively make it disabled without using the disabled attribute.

The most basic property is pointer-events: none;. This makes the element unable to register clicks or hovers. I also recommend reducing the opacity so the button renders as a lighter color.

.disabled {
  pointer-events: none;
  opacity: .65;

<button class="disabled" >CSS Permanent Disable</button>

These can easily be applied using a class on the element.

You may have noticed that I recommended adding both of these styles in the previous section where we set the button attribute disabled=true. Styling with these properties is good practice on any disabled button.

One disadvantage of using a CSS approach is that users can still tab in to the button and ‘click’ it by pressing enter. Interestingly, this first triggers a keypress event and then triggers a click event.

The two solutions to this are setting tabindex to -1 or adding a keypress event listener and preventing the default behavior. Both are described below.

Programmatically Disable Button with CSS Class

Toggling a CSS class on an HTML button element is similar to toggling the disabled attribute.

Once again, we select the button and attach a click event listener. Then in the click handler we need to add the disabled class using classList.add("disabled").

<button id="cssDisable" type="button" style="margin: 12px;">
  CSS Toggle Disable

const handleCSSBtnClick = (
) => {

const cssDisabledButton = document.getElementById("cssDisable");
if (cssDisabledButton) {
  cssDisabledButton.addEventListener("click", (event) => {
    handleCSSBtnClick(event, cssDisabledButton);
  cssDisabledButton.addEventListener("keypress", (event) => {

I also included the code for adding a keypress listener in case the user tabs into the button and presses enter. Later I will show how to eliminate tabbing.

Disabled Button ARIA Considerations

Users requiring screen readers and non-mouse users need to be supported when a button is disabled.

Screen readers need the aria-disabled attribute to be set to true on the button. This indicates that the element is perceivable but not operable, according to MDN.

Next, the tabIndex attribute needs to be set to -1 while the button is disabled. This keeps users from tabbing into a disabled element and interacting with it through the keyboard.

Here’s an example button that has both attributes set.

  style="margin: 12px;"
  CSS Permanent Disable


Here are some useful related posts:

Code Sandbox Link (This includes TypeScript as well)

Share this post:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.