25
var disableSelection = function(){
    $("#elementId").attr("disabled","disabled");    
};

var enableSelection = function(){
    $("#elementId").removeAttr("disabled");
};

I have following requirements:

  1. disable SELECT element in order to restrict users from selecting options
  2. disabled SELECT element has some OPTION element already selected
  3. on submit event enable disabled SELECT element in order to save selected value

Adding attribute "disabled" works well for IE. Yet, when I try to remove attribute by using jQuery revomeAttr() method then instead of removing attribute:

  1. method adds "disabled" attribute to previously enabled SELECT element
  2. method doesn't remove "disabled" attribute
James
  • 24,676
  • 13
  • 84
  • 130
Maris Zemgalis
  • 263
  • 1
  • 3
  • 7
  • 3
    How about `$("#elementId").attr("disabled","");`? – Felix Kling Apr 20 '12 at 07:46
  • Is it perhaps re-disabling the select box somewhere else in your code? without more of a context it is very hard to tell. – Qpirate Apr 20 '12 at 07:52
  • 1
    setting $("#elementId").attr("disabled",""); disables element in IE & Firefox .. also setting $("#elementId").attr("disabled", false) won't have any effect on IE. – Maris Zemgalis Apr 20 '12 at 08:05
  • I am surprise with it's not working. Because i believe "$("#elementId").removeAttr("disabled");" is the better way to enable then other. Are you sure it's not working may be you are getting error because of any other script. – Tarun Apr 20 '12 at 08:29
  • 1
    possible duplicate of [Remove disabled attribute using JQuery?](http://stackoverflow.com/questions/13626517/remove-disabled-attribute-using-jquery) – user2381114 Apr 29 '14 at 13:04

3 Answers3

56

Use .prop instead of .attr to affect an element's disabled state:

var disableSelection = function(){
    $("#elementId").prop("disabled", true);    
};

var enableSelection = function(){
    $("#elementId").prop("disabled", false);
};

For more information, see .prop() vs .attr().

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • 6
    I know this was posted back in '12, but now (in 2015), if you are using IE11, the `prop` solution does not fix the problem. Any updates on this? – cidthecoatrack Feb 16 '15 at 16:57
  • @cidthecoatrack: [Works for me](http://jsfiddle.net/Lmpgsmka/) in IE9, IE10 and edge rendering modes. – Jon Apr 14 '15 at 19:00
4

You can use removeProp('disabled') instead. It worked for me while removeAttr('disabled') didn't.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
1

Jquery Docs says, about .removeProp():

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.

Additional Notes:

In Internet Explorer prior to version 9, using .prop() to set a DOM element >property to anything other than a simple primitive value (number, string, or >boolean) can cause memory leaks if the property is not removed (using >.removeProp()) before the DOM element is removed from the document. To safely >set values on DOM objects without memory leaks, use .data().

Community
  • 1
  • 1
RodrigoB
  • 11
  • 1