Disable/enable an input with jQuery?

JavascriptJqueryHtml InputDisabled Input

Javascript Problem Overview


$input.disabled = true;

or

$input.disabled = "disabled";

Which is the standard way? And, conversely, how do you enable a disabled input?

Javascript Solutions


Solution 1 - Javascript

jQuery 1.6+

To change the disabled property you should use the .prop() function.

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

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

Set the disabled attribute.

$("input").attr('disabled','disabled');

To enable again, the proper method is to use .removeAttr()

$("input").removeAttr('disabled');

In any version of jQuery

You can always rely on the actual DOM object and is probably a little faster than the other two options if you are only dealing with one element:

// assuming an event handler thus 'this'
this.disabled = true;

The advantage to using the .prop() or .attr() methods is that you can set the property for a bunch of selected items.


Note: In 1.6 there is a .removeProp() method that sounds a lot like removeAttr(), but it SHOULD NOT BE USED on native properties like 'disabled' Excerpt from the documentation:

> Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.

In fact, I doubt there are many legitimate uses for this method, boolean props are done in such a way that you should set them to false instead of "removing" them like their "attribute" counterparts in 1.5

Solution 2 - Javascript

Just for the sake of new conventions && making it adaptable going forward (unless things change drastically with ECMA6(????):

$(document).on('event_name', '#your_id', function() {
    $(this).removeAttr('disabled');
});

and

$(document).off('event_name', '#your_id', function() {
    $(this).attr('disabled','disabled');   
});

Solution 3 - Javascript

    // Disable #x
    $( "#x" ).prop( "disabled", true );
    // Enable #x
    $( "#x" ).prop( "disabled", false );

Sometimes you need to disable/enable the form element like input or textarea. Jquery helps you to easily make this with setting disabled attribute to "disabled". For e.g.:

  //To disable 
  $('.someElement').attr('disabled', 'disabled');

To enable disabled element you need to remove "disabled" attribute from this element or empty it's string. For e.g:

//To enable 
$('.someElement').removeAttr('disabled');

// OR you can set attr to "" 
$('.someElement').attr('disabled', '');

refer :http://garmoncheg.blogspot.fr/2011/07/how-to-disableenable-element-with.html

Solution 4 - Javascript

$("input")[0].disabled = true;

or

$("input")[0].disabled = false;

Solution 5 - Javascript

There are many ways using them you can enable/disable any element :

Approach 1

$("#txtName").attr("disabled", true);

Approach 2

$("#txtName").attr("disabled", "disabled");

If you are using jQuery 1.7 or higher version then use prop(), instead of attr().

$("#txtName").prop("disabled", "disabled");

If you wish to enable any element then you just have to do opposite of what you did to make it disable. However jQuery provides another way to remove any attribute.

Approach 1

$("#txtName").attr("disabled", false);

Approach 2

$("#txtName").attr("disabled", "");

Approach 3

$("#txtName").removeAttr("disabled");

Again, if you are using jQuery 1.7 or higher version then use prop(), instead of attr(). That's is. This is how you enable or disable any element using jQuery.

Solution 6 - Javascript

You can put this somewhere global in your code:

$.prototype.enable = function () {
    $.each(this, function (index, el) {
        $(el).removeAttr('disabled');
    });
}

$.prototype.disable = function () {
    $.each(this, function (index, el) {
        $(el).attr('disabled', 'disabled');
    });
}

And then you can write stuff like:

$(".myInputs").enable();
$("#otherInput").disable();

Solution 7 - Javascript

If you just want to invert the current state (like a toggle button behaviour):

$("input").prop('disabled', ! $("input").prop('disabled') );

Solution 8 - Javascript

Use like this,

 $( "#id" ).prop( "disabled", true );
     
 $( "#id" ).prop( "disabled", false );

Solution 9 - Javascript

Update for 2018:

Now there's no need for jQuery and it's been a while since document.querySelector or document.querySelectorAll (for multiple elements) do almost exactly same job as $, plus more explicit ones getElementById, getElementsByClassName, getElementsByTagName

Disabling one field of "input-checkbox" class

document.querySelector('.input-checkbox').disabled = true;

or multiple elements

document.querySelectorAll('.input-checkbox').forEach(el => el.disabled = true);

Solution 10 - Javascript

You can use the jQuery prop() method to disable or enable form element or control dynamically using jQuery. The prop() method require jQuery 1.6 and above.

Example:

<script type="text/javascript">
        $(document).ready(function(){
            $('form input[type="submit"]').prop("disabled", true);
            $(".agree").click(function(){
                if($(this).prop("checked") == true){
                    $('form input[type="submit"]').prop("disabled", false);
                }
                else if($(this).prop("checked") == false){
                    $('form input[type="submit"]').prop("disabled", true);
                }
            });
        });
    </script>

Solution 11 - Javascript

this works for me

$("#values:input").attr("disabled",true);
$("#values:input").attr("disabled",false);

Solution 12 - Javascript

Disable true for input type :

> In case of a specific input type (Ex. Text type input)

$("input[type=text]").attr('disabled', true);

> For all type of input type

$("input").attr('disabled', true);

Solution 13 - Javascript

Disable:

$('input').attr('readonly', true); // Disable it.
$('input').addClass('text-muted'); // Gray it out with bootstrap.

Enable:

$('input').attr('readonly', false); // Enable it.
$('input').removeClass('text-muted'); // Back to normal color with bootstrap.

Solution 14 - Javascript

An alternate way to disable the input field is by using jQuery and css like this:

jQuery("#inputFieldId").css({"pointer-events":"none"})

and to enable the same input the code is as follows:

jQuery("#inputFieldId").css({"pointer-events":""})

Solution 15 - Javascript

<html>
<body>

Name: <input type="text" id="myText">



<button onclick="disable()">Disable Text field</button>
<button onclick="enable()">Enable Text field</button>

<script>
function disable() {
    document.getElementById("myText").disabled = true;
}
function enable() {
    document.getElementById("myText").disabled = false;
}
</script>

</body>
</html>

Solution 16 - Javascript

I used @gnarf answer and added it as function

   $.fn.disabled = function (isDisabled) {
     if (isDisabled) {
       this.attr('disabled', 'disabled');
     } else {
       this.removeAttr('disabled');
     }
   };

Then use like this

$('#myElement').disable(true);

Solution 17 - Javascript

2018, without JQuery (ES6)

Disable all input:

[...document.querySelectorAll('input')].map(e => e.disabled = true);

Disable input with id="my-input"

document.getElementById('my-input').disabled = true;

The question is with JQuery, it's just FYI.

Solution 18 - Javascript

Approach 4 (this is extension of wild coder answer)

txtName.disabled=1     // 0 for enable

<input id="txtName">

Solution 19 - Javascript

In jQuery Mobile:

###For disable

$('#someselectElement').selectmenu().selectmenu('disable').selectmenu('refresh', true);
$('#someTextElement').textinput().textinput('disable');

###For enable

$('#someselectElement').selectmenu().selectmenu('enable').selectmenu('refresh', true);
$('#someTextElement').textinput('enable');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionomgView Question on Stackoverflow
Solution 1 - JavascriptgnarfView Answer on Stackoverflow
Solution 2 - JavascriptgeekbuntuView Answer on Stackoverflow
Solution 3 - JavascriptHarini SekarView Answer on Stackoverflow
Solution 4 - JavascriptSajjad ShirazyView Answer on Stackoverflow
Solution 5 - Javascriptwild coderView Answer on Stackoverflow
Solution 6 - JavascriptNicu SurduView Answer on Stackoverflow
Solution 7 - JavascriptdaVeView Answer on Stackoverflow
Solution 8 - JavascriptAbdus Salam AzadView Answer on Stackoverflow
Solution 9 - JavascriptPawelView Answer on Stackoverflow
Solution 10 - JavascriptSPARTANView Answer on Stackoverflow
Solution 11 - JavascriptSiddharthaView Answer on Stackoverflow
Solution 12 - JavascriptHasib Kamal ChowdhuryView Answer on Stackoverflow
Solution 13 - JavascriptTomer Ben DavidView Answer on Stackoverflow
Solution 14 - Javascriptmukhtar alamView Answer on Stackoverflow
Solution 15 - Javascriptuser3831708View Answer on Stackoverflow
Solution 16 - JavascriptImants VolkovsView Answer on Stackoverflow
Solution 17 - Javascriptrap-2-hView Answer on Stackoverflow
Solution 18 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 19 - JavascriptAtif HussainView Answer on Stackoverflow