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 elementdisabledis a property-based state, not just a text attribute- It avoids confusion that can happen when using for boolean values
Mental Model
Think of an input like a door with a lock.
- When
disabledistrue, the door is locked - When
disabledisfalse, 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.
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.();
input. = ;
input. = ;
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:
- The page loads with the input enabled by default.
- The user clicks the Toggle button.
- The click handler runs.
$('#username').prop('disabled')reads the current disabled state.- If the input is enabled, this returns
false - If the input is disabled, this returns
true
- If the input is enabled, this returns
!isDisabledflips the value.
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.
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').(, updateSubmitState);
();
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:
$inputis a jQuery object, not the raw DOM element- jQuery objects do not use
.disableddirectly
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(, );
Comparisons
Here is how common approaches compare.
| Approach | Example | Works for disabling? | Best use | Notes |
|---|---|---|---|---|
| Native DOM property | input.disabled = true | Yes | Plain JavaScript | Direct and simple |
jQuery .prop() | $('#name').prop('disabled', true) | Yes | jQuery code | Preferred jQuery approach |
jQuery .attr() | $('#name').attr('disabled', 'disabled') | Yes, but older style | Legacy code | Less ideal for live boolean state |
| jQuery |
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
disabledis a boolean property- In jQuery, prefer
.prop()over.attr()for boolean properties $('#id')returns a jQuery object, not a raw DOM element- Use
trueto disable andfalseto enable - Disabled form controls are usually with the form
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 = ;
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
- Add a checkbox labeled "Provide email address".
- Add an email input that starts in the disabled state.
- When the checkbox is checked, enable the email input.
- When the checkbox is unchecked, disable the email input again.
- Update the state immediately when the checkbox changes.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.