Blog

How to disable button in javascript

Illustration of a text input and a submit button changing from disabled to enabled

Enable a submit button after a visitor enters required text with a small JavaScript input handler. This tutorial covers the native disabled property, whitespace checks, accessible feedback, and form submission.

Privacy: The demo intercepts submission and sends nothing. Use made-up sample text only.

Try the live demo   Download source ZIP

Interactive example

Enable the button when the field has text

Type a made-up name. The button becomes active when the field contains at least one non-space character. Submitting is intercepted; nothing is sent.

The button is disabled until you enter text.

Enter a name to enable the button.

Privacy: This demo runs locally and does not submit or save the field value. Use sample text only.

How the disabled button works

HTML buttons include a native disabled state. When set, the browser prevents normal activation and communicates that state to assistive technology. JavaScript updates the property as the field value changes.

Build the accessible form

Connect a visible label to the text field, use a real submit button, and explain what the visitor needs to do. A polite live status can confirm that the control is ready.

<label for="visitor-name">Your name</label>
<input id="visitor-name" type="text" aria-describedby="name-hint">
<p id="name-hint">Enter a name to enable Submit.</p>
<button id="submit-button" type="submit" disabled>Submit</button>

Enable the button with JavaScript

Use the input event to cover typing, pasting, and other ways the field may change. Trim whitespace so spaces alone do not enable submission.

const nameInput = document.querySelector('#visitor-name');
const submitButton = document.querySelector('#submit-button');
nameInput.addEventListener('input', () => {
  submitButton.disabled = nameInput.value.trim().length === 0;
});

Handle submission and validation

Disabling a button is a convenience, not validation. Check the value again when submitting. This example prevents navigation so no data leaves the page:

form.addEventListener('submit', (event) => {
  event.preventDefault();
  if (!nameInput.value.trim()) return;
  status.textContent = 'Demo complete. No data was sent.';
});

A real application should validate submitted values on the server and show a clear error when a required field is empty.

Common questions

Why use input instead of keyup?

The input event also handles paste and other field changes that key events can miss.

Should spaces enable the button?

Usually no for a required text field. Use trim() so whitespace only remains empty.

Does a disabled button replace validation?

No. Validate again on submission, and validate on the server whenever a real form sends data.

Conclusion

Keep a submit button in sync with a required field using the native disabled property and the input event. Add a visible explanation and validate again on submission. Try the demo, then download the standalone files.

Try the live demo   Download source ZIP