486

I have to disable inputs at first and then on click of a link to enable them.

This is what I have tried so far, but it doesn't work.

HTML:

<input type="text" disabled="disabled" class="inputDisabled" value="">

jQuery:

$("#edit").click(function(event){
   event.preventDefault();
   $('.inputDisabled').removeAttr("disabled")
});


This shows me true and then false but nothing changes for the inputs:

$("#edit").click(function(event){
   alert('');
   event.preventDefault();
   alert($('.inputDisabled').attr('disabled'));
   $('.inputDisabled').removeAttr("disabled");
   alert($('.inputDisabled').attr('disabled'));
});
informatik01
  • 16,038
  • 10
  • 74
  • 104
fatiDev
  • 5,752
  • 7
  • 28
  • 45

11 Answers11

978

Always use the prop() method to enable or disable elements when using jQuery (see below for why).

In your case, it would be:

$("#edit").click(function(event){
   event.preventDefault();
   $('.inputDisabled').prop("disabled", false); // Element(s) are now enabled.
});

jsFiddle example here.


Why use prop() when you could use attr()/removeAttr() to do this?

Basically, prop() should be used when getting or setting properties (such as autoplay, checked, disabled and required amongst others).

While what you want to do can technically be done using attr()/removeAttr(), it doesn't mean it should be done - and can cause strange/problematic behaviour, as in this case.

"The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes."

"Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value." - jQuery documentation for prop()

Pre-jQuery 3.0 (before 2016)

The reason why you should use prop over removeAttr() is that removeAttr() completely removes the disabled attribute itself - as this method would simply set the corresponding property name to false:

Prior to jQuery 3.0, using .removeAttr() on a boolean attribute such as checked, selected, or readonly would also set the corresponding named property to false. This behavior was required for ancient versions of Internet Explorer but is not correct for modern browsers because the attribute represents the initial value and the property represents the current (dynamic) value. - jQuery 3.0 Breaking Changes

While prop() merely sets the property's underlying boolean value to false.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • 3
    People should also know abut the dirty checkedness flag, which completely breaks `attr` vs `prop` for checkboxes: http://www.w3.org/TR/html5/forms.html#concept-input-checked-dirty except for internal jQuery workaround magic. – Ciro Santilli OurBigBook.com Jul 06 '14 at 11:29
  • 1
    Just a little addition, if you want to disable the checkbox as well as want to uncheck it. We need to uncheck at first and then disable like this jQuery('#no_tax_payer').prop("checked", false).prop("disabled", true); Hope this helps someone ! – Haijerome Jul 22 '14 at 05:36
  • 35
    Hmm, .prop("disabled", false) doesn't seem to work for me in a button - leaves the 'disabled' attribute in the tag with no value. – UpTheCreek Jan 06 '15 at 10:42
  • 5
    @UpTheCreek was not working for me too. But I discovered to be a scope problem with the 'this' clause (as usual). In the specific case `.prop()` was executed on the element pointed by `this` (`this.prop("disabled", false)`, but being into a callback it was binding to the wrong element, so I had to do the common `var that = this` workaround. Trying to play with `.apply()`/`.call()` was not working well; I don't know why. Double check with a `console.log(this)` if it is set to your desired object, just before calling `this.prop(...)` – Kamafeather Jan 22 '15 at 13:35
  • 4
    I nominate this answer as a candidate for conversion over to the document section. Clear, complete, well-written. – Anne Gunn Sep 06 '16 at 17:35
  • .prop("disabled", false) does not work, the result is disable="", and the field stays disabled. The solution for me was $('#field').removeAttr('disabled'); – Jam Mar 20 '18 at 16:56
  • 5
    _By using removeAttr(), you are completely removing the disabled attribute itself - while prop() is merely setting the property's underlying boolean value to false._ I don't believe this to be true. In HTML, the mere presence of the `disabled` property will disable the element. For example, having a `` will not enable the button, the property must be removed. By calling `$('button').prop('disabled', false);` jQuery will remove the property for you, as I observed in [this codepen](https://codepen.io/anon/pen/aMJELv) (inspect to see the property removal). – Naftali Mar 07 '19 at 14:25
75

to remove disabled attribute use,

 $("#elementID").removeAttr('disabled');

and to add disabled attribute use,

$("#elementID").prop("disabled", true);

Enjoy :)

Umesh Patil
  • 4,668
  • 32
  • 24
45
<input type="text" disabled="disabled" class="inputDisabled" value="">
​<button id="edit">Edit</button>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

$("#edit").click(function(event){
    event.preventDefault();
    $('.inputDisabled').removeAttr("disabled")
});​

http://jsfiddle.net/ZwHfY/

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
17

Use like this,

HTML:

<input type="text" disabled="disabled" class="inputDisabled" value="">

<div id="edit">edit</div>

JS:

 $('#edit').click(function(){ // click to
            $('.inputDisabled').attr('disabled',false); // removing disabled in this class
 });
Dhamu
  • 1,694
  • 5
  • 22
  • 47
9

I think you are trying to toggle the disabled state, in witch case you should use this (from this question):

$(".inputDisabled").prop('disabled', function (_, val) { return ! val; });

Here is a working fiddle.

Community
  • 1
  • 1
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
8

for removing the disabled properties

 $('#inputDisabled').removeAttr('Disabled');

for adding the disabled properties

 $('#inputDisabled').attr('disabled', 'disabled' );
Mosam Prajapati
  • 189
  • 2
  • 6
  • 1
    That answer has been given previously, but it's still wrong. You're calling attributes "properties", which is confusing. – Auspex Nov 18 '20 at 12:47
5

2018, without JQuery

I know the question is about JQuery: this answer is just FYI.

document.getElementById('edit').addEventListener(event => {
    event.preventDefault();
    [...document.querySelectorAll('.inputDisabled')].map(e => e.disabled = false);
}); 
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
3

Thought this you can easily setup

$(function(){
$("input[name^=radio_share]").click
(
    function()
    {
        if($(this).attr("id")=="radio_share_dependent")
        {
            $(".share_dependent_block input, .share_dependent_block select").prop("disabled",false);   
        }
        else
        {
            $(".share_dependent_block input, .share_dependent_block select").prop("disabled",true);   
        }
    }
 );
});
Er.gaurav soni
  • 157
  • 1
  • 5
3

This was the only code that worked for me:

element.removeProp('disabled')

Note that it's removeProp and not removeAttr.

I'm using jQuery 2.1.3 here.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 13
    From jQuery documentation: Important: the .removeProp() method should not be used to set these properties to false. Once a native property is removed, it cannot be added again. See .removeProp() for more information. – XanatosLightfiren Jun 15 '15 at 09:00
1

This question specifically mentions jQuery, but if you are looking to accomplish this without jQuery, the equivalent in vanilla JavaScript is:

elem.removeAttribute('disabled');
jdgregson
  • 1,457
  • 17
  • 39
0

Try special selector:

Not working : $('#ID_Unit').removeAttr("disabled");
Works : $('select[id=ID_Unit]:disabled').removeAttr("disabled");

all "select" controls $('select:disabled').removeAttr("disabled");

"select" is control type like "type" etc.