10

With the release of jQuery 1.6, the recommendation on SO has been to generally start using prop() where you used to use attr().

What happens when I want to disable an element?

$('.control').prop('disabled', 'disabled');
$('.control').prop('disabled', true);

Neither of these seem to disable the control. Is disabling an element the exception to the rule?

UPDATE And turns out the reason the element wasn't being disabled was because of a line I had that ran before the lines above:

$('.control').removeProp('disabled');

In enabling controls, I had got used to using .removeAttr() so figured .removeProp would be enough. Instead, use the following to enable controls:

$('.control').prop('disabled', false);
ajbeaven
  • 9,265
  • 13
  • 76
  • 121
  • 1
    What kind of element are you trying to disable? – Matt Ball May 05 '11 at 00:51
  • Regarding your update, it works for me too: http://jsfiddle.net/fkling/K95Fr/1/ – Felix Kling May 05 '11 at 01:02
  • Can you answer your own question in a way that would help others? If you do, you can select yours as the correct answer. It may seem strange, but it is the preferred way of dealing with situations like this. –  May 06 '11 at 15:43
  • I need to wait a number of hours before I can accept my own answer. – ajbeaven May 06 '11 at 23:08

3 Answers3

13
$('.control').prop('disabled', true);

works fine for me.

Alternatively use

$('.control').attr('disabled', 'disabled');

Update:

But even $('.control').prop('disabled', 'disabled'); or $('.control').attr('disabled', true); disable the element too. So if it does not work for you, the question is really which element you are trying to disable. Can it be even disabled?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

Make sure you haven't .removeProp('disabled') on the element you're trying to disable.

ajbeaven
  • 9,265
  • 13
  • 76
  • 121
1

Use .prop('disabled', true).

http://jsfiddle.net/mattball/BaLHf/

Matt Ball
  • 354,903
  • 100
  • 647
  • 710