Question

How to Disable and Enable an Input with jQuery

javascriptjqueryhtml-inputdisabled-input

Question

I want to disable an input element using jQuery. I have seen examples like:

$input.disabled = true;

or:

$input.disabled = "disabled";

Which approach is the standard one?

Also, how do you enable an input again after it has been disabled?

Short Answer

By the end of this page, you will understand how the disabled state works for form inputs, the difference between DOM properties and HTML attributes, and the correct jQuery way to disable or enable an input using .prop(). You will also see common mistakes, real-world usage patterns, and a small project you can build to practice.

Concept

The main concept behind this question is controlling boolean properties on DOM elements.

In HTML, some attributes are boolean attributes. disabled is one of them. That means the element is considered disabled when the attribute/property is present or set to true.

For example:

<input disabled>

This input is disabled even though no value is assigned.

In JavaScript, DOM elements expose disabled as a property:

element.disabled = true;

That is the native JavaScript way.

In jQuery, the standard modern approach is:

$(element).prop('disabled', true);

Why .prop() matters:

  • .prop() works with the current live property value of the DOM element
  • disabled is a property-based state, not just a text attribute
  • It avoids confusion that can happen when using .attr() for boolean values

To enable the input again:

$(element).prop('disabled', false);

This matters in real programming because forms often need to react to user actions:

  • disable a submit button while a request is in progress
  • disable fields until a checkbox is checked
  • prevent editing of calculated values
  • temporarily lock controls during validation or loading

So the key lesson is:

  • Native JavaScript: element.disabled = true or false
  • jQuery: $(element).prop('disabled', true) or false

Mental Model

Think of an input like a door with a lock.

  • When disabled is true, the door is locked
  • When disabled is false, the door is unlocked

The important part is that the browser keeps track of the lock as a real state on the element.

Using .prop('disabled', true) is like changing the actual lock status.

Using .attr('disabled', 'disabled') is more like sticking a label on the door. That may reflect the state in HTML, but what you usually care about in code is the live state of the element right now.

So for interactive code, think property = actual current state.

Take Quiz

Syntax and Examples

The recommended jQuery syntax is:

$('#myInput').prop('disabled', true);  // disable
$('#myInput').prop('disabled', false); // enable

Example: disable an input

<input id="name" type="text" />
$('#name').prop('disabled', true);

This makes the input unusable.

Example: enable an input

$('#name').prop('disabled', false);

This makes the input usable again.

Native JavaScript equivalent

const input = document.getElementById('name');
input.disabled = true;
input.disabled = false;

Older jQuery style you may still see

$('#name').attr('disabled', 'disabled');
$('#name').removeAttr('disabled');

This was common in older code, but for boolean properties like disabled, checked, and selected, .prop() is the clearer and preferred approach in modern jQuery.

Example with a button click

<input id="email" type="email" />
<button id="disableBtn">Disable</button>
<button id="enableBtn">Enable</button>
$('#disableBtn').on('click', function () {
  $('#email').prop('disabled', true);
});

$('#enableBtn').on('click', function () {
  $('#email').prop('disabled', false);
});

This is a common pattern in interactive forms.

Step by Step Execution

Consider this code:

<input id="username" type="text" value="alice" />
<button id="toggle">Toggle</button>
$('#toggle').on('click', function () {
  const isDisabled = $('#username').prop('disabled');
  $('#username').prop('disabled', !isDisabled);
});

Here is what happens step by step:

  1. The page loads with the input enabled by default.
  2. The user clicks the Toggle button.
  3. The click handler runs.
  4. $('#username').prop('disabled') reads the current disabled state.
    • If the input is enabled, this returns false
    • If the input is disabled, this returns true
  5. !isDisabled flips the value.
    • false becomes true
    • true becomes false
  6. $('#username').prop('disabled', !isDisabled) updates the input.
  7. Each new click repeats the process.

Trace example

Initial state:

isDisabled = false
!isDisabled = true

Result: the input becomes disabled.

Next click:

isDisabled = true
!isDisabled = false

Result: the input becomes enabled again.

This shows both reading and writing a boolean property with jQuery.

Real World Use Cases

Disabling and enabling inputs appears frequently in real applications.

Form submission locking

When a user clicks Submit, you may disable the button to prevent duplicate requests.

$('#submitBtn').prop('disabled', true);

Conditional fields

A shipping address field might only become editable when a checkbox is selected.

$('#useDifferentAddress').on('change', function () {
  $('#shippingAddress').prop('disabled', !this.checked);
});

Loading state

While data is being fetched from an API, inputs may be temporarily disabled.

$('#search').prop('disabled', true);
// fetch data...
$('#search').prop('disabled', false);

Role-based UI restrictions

Some fields may be shown but disabled for users without permission to edit them.

Computed values

A total-price field may be displayed in a form but disabled so users cannot manually change it.

Take Quiz

Real Codebase Usage

In real projects, developers usually combine disabled with other patterns.

Guarding against repeated actions

$('#saveBtn').on('click', function () {
  const $btn = $(this);

  if ($btn.prop('disabled')) {
    return;
  }

  $btn.prop('disabled', true);

  setTimeout(function () {
    $btn.prop('disabled', false);
  }, 2000);
});

This prevents double clicks.

Validation-driven enabling

function updateSubmitState() {
  const hasEmail = $('#email').val().trim() !== '';
  $('#submitBtn').prop('disabled', !hasEmail);
}

$('#email').on('input', updateSubmitState);
updateSubmitState();

This keeps the submit button disabled until required data is present.

Early return pattern

Developers often check state first, then return early if the action should not continue.

if ($('#name').prop('disabled')) {
  return;
}

Async request handling

$('#loadBtn').on('click', function () {
  const $btn = $(this);
  $btn.prop('disabled', true);

  $.ajax({
    url: '/api/data',
    method: 'GET'
  }).always(function () {
    $btn.prop('disabled', false);
  });
});

Using .always() ensures the control is re-enabled whether the request succeeds or fails.

Centralized UI state updates

In larger codebases, teams often put enable/disable logic inside helper functions to keep code consistent.

function setFormLocked(locked) {
  $('#name, #email, #saveBtn').prop('disabled', locked);
}

Common Mistakes

Here are common beginner mistakes when working with disabled inputs.

Mistake 1: Treating a jQuery object like a DOM element

Broken code:

const $input = $('#name');
$input.disabled = true;

Why it fails:

  • $input is a jQuery object, not the raw DOM element
  • jQuery objects do not use .disabled directly

Correct approaches:

$('#name').prop('disabled', true);

or:

const input = $('#name')[0];
input.disabled = true;

Mistake 2: Using .attr() when .prop() is clearer

Older code:

$('#name').attr('disabled', 'disabled');

This may work, but for live boolean state, .prop() is preferred.

Better:

$('#name').prop('disabled', true);

Mistake 3: Trying to enable with a string value

Broken idea:

$('#name').prop('disabled', 'disabled');

Use booleans instead:

$('#name').prop('disabled', true);
$('#name').prop('disabled', false);

Mistake 4: Forgetting to re-enable after async work

Broken pattern:

$('#saveBtn').prop('disabled', true);
// request starts, but never re-enables on error

Better pattern:

$('#saveBtn').prop('disabled', true);

$.ajax({
  url: '/save'
}).always(function () {
  $('#saveBtn').prop('disabled', false);
});

Mistake 5: Assuming disabled fields submit with the form

Disabled fields are generally not submitted with form data.

If you need the value submitted, consider:

  • using readonly instead of disabled for text inputs when appropriate
  • syncing the value into a hidden input

Mistake 6: Confusing readonly with disabled

  • readonly means the user cannot edit the value, but the field may still be focusable and submitted
  • disabled means the control is inactive and usually excluded from form submission

Comparisons

Here is how common approaches compare.

ApproachExampleWorks for disabling?Best useNotes
Native DOM propertyinput.disabled = trueYesPlain JavaScriptDirect and simple
jQuery .prop()$('#name').prop('disabled', true)YesjQuery codePreferred jQuery approach
jQuery .attr()$('#name').attr('disabled', 'disabled')Yes, but older styleLegacy codeLess ideal for live boolean state
jQuery .removeAttr()$('#name').removeAttr('disabled')Yes, for enablingLegacy codeOlder alternative to .prop('disabled', false)

disabled vs readonly

Featuredisabledreadonly
User can edit valueNoNo
User can focus fieldUsually noYes
Included in form submissionUsually noYes
Good for fully inactive controlsYesSometimes

Which one should you use?

  • Use .prop('disabled', true/false) in jQuery code
  • Use element.disabled = true/false in plain JavaScript
  • Use readonly instead of disabled only when you want the value to remain part of form submission and still behave like a readable field
Take Quiz

Cheat Sheet

// jQuery: disable
$('#myInput').prop('disabled', true);

// jQuery: enable
$('#myInput').prop('disabled', false);

// jQuery: read current state
const isDisabled = $('#myInput').prop('disabled');

// Native JavaScript: disable
const input = document.getElementById('myInput');
input.disabled = true;

// Native JavaScript: enable
input.disabled = false;

Rules to remember

  • disabled is a boolean property
  • In jQuery, prefer .prop() over .attr() for boolean properties
  • $('#id') returns a jQuery object, not a raw DOM element
  • Use true to disable and false to enable
  • Disabled form controls are usually not submitted with the form

Common old-style patterns

$('#myInput').attr('disabled', 'disabled');
$('#myInput').removeAttr('disabled');

Still seen in older code, but modern jQuery code usually uses .prop().

Related boolean properties

  • checked
  • selected
  • disabled

These are usually best handled with .prop() in jQuery.

FAQ

Should I use .attr() or .prop() for disabled in jQuery?

Use .prop() for disabled. It reflects the current live state of the element and is the preferred jQuery approach for boolean properties.

How do I enable a disabled input in jQuery?

Use:

$('#myInput').prop('disabled', false);

Why does $input.disabled = true not work with jQuery?

Because $input is usually a jQuery object. The disabled property belongs to the actual DOM element, not the jQuery wrapper.

Can I still use attr('disabled', 'disabled')?

Yes, older code often does this. But for modern jQuery code, .prop('disabled', true) is clearer and more correct for boolean state.

What is the plain JavaScript way to disable an input?

Use:

element.disabled = true;

And to enable it:

element.disabled = false;

What is the difference between disabled and readonly?

A disabled field is inactive and usually not submitted with the form. A readonly field cannot be edited, but its value is usually still submitted.

Can I disable multiple inputs at once with jQuery?

Yes.

$('input').prop('disabled', true);

This applies the property to every matched input.

Take Quiz

Mini Project

Description

Build a small form that keeps an email input disabled until the user checks a box agreeing to provide a contact email. This demonstrates how to read checkbox state, update input state with jQuery, and keep the UI behavior in sync with user actions.

Goal

Create a form where a checkbox controls whether an email field is enabled or disabled.

Requirements

  1. Add a checkbox labeled "Provide email address".
  2. Add an email input that starts in the disabled state.
  3. When the checkbox is checked, enable the email input.
  4. When the checkbox is unchecked, disable the email input again.
  5. Update the state immediately when the checkbox changes.
Take Quiz

Keep learning

Related questions

View more