How to disable and enable input using jQuery: a complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to disable and enable input using jQuery? A guide for beginners and beyond



Imagine a situation: you're creating a registration form, and until the user selects a country, the "City" field should be blocked. Or you have an online store, and the "Place Order" button is inactive until a consent checkbox is checked. Sound familiar? This task is the bread and butter of any frontend developer. And today we'll examine it under a microscope.



The question "Disable/enable an input with jQuery?" is one of the most popular on Stack Overflow (thousands of views and dozens of answers). And this is no coincidence: the apparent simplicity hides several pitfalls that beginners stumble upon. In this guide, we won't just show code — we'll teach you to think like a jQuery ninja. Let's go!



Why shouldn't you use .attr() for disabled?



If you've already Googled a solution, you've probably seen old advice like $('input').attr('disabled', 'disabled');. This worked in the jQuery 1.x era, but today — it's a dead end. Why?



  • .attr() works with HTML attributes, while .prop() works with DOM object properties.
  • The disabled attribute is a boolean property that should be true or false, not a string.
  • When using .attr('disabled', false), jQuery may simply remove the attribute but not always correctly reset the state.


The modern standard is to use .prop(). Remember this like the "Our Father" prayer:



// Disable input$('#myInput').prop('disabled', true);

// Enable input back$('#myInput').prop('disabled', false);


Simple, right? But let's dig deeper.



Basic examples: disabling and enabling a single input



Let's say we have an email input field and a trigger button. We want the field to be locked or unlocked when the button is clicked.



<input type="email" id="emailInput" placeholder="Enter email"><button id="toggleBtn">Lock field</button>

<script>$(document).ready(function() { $('#toggleBtn').on('click', function() { var $input = $('#emailInput'); var isDisabled = $input.prop('disabled'); // Toggle state $input.prop('disabled', !isDisabled); // Change button text $(this).text(isDisabled ? 'Lock field' : 'Unlock field'); });});</script>


Notice: we use $(document).ready() — this ensures the DOM is loaded before the script executes. Without this, your code might not find the elements.



Working with a group of elements: select, checkbox, and textarea



The .prop() method is universal. It works not only with <input>, but also with <select>, <textarea>, <button>, and even entire sets of elements.



Blocking all form elements at once



Imagine you need to lock the entire form after submission so the user cannot change the data:



<form id="myForm">    <input type="text" name="name" placeholder="Name">    <input type="email" name="email" placeholder="Email">    <select name="country">        <option value="">Select a country</option>        <option value="ru">Russia</option>        <option value="us">USA</option>    </select>    <button type="submit">Submit</button></form>

<script>// Block all input, select, and textarea inside the form$('#myForm').find('input, select, textarea, button').prop('disabled', true);</script>


You can be even more elegant: use :input — this is a jQuery pseudo-selector that selects all elem

Blogs

Book Recommendations