29

I can't seem to get removeAttr to work, I'm using the example I saw on the jQuery site. Basically onclick I add the attribute to disable a field (which works just fine) but when the user clicks again it should enable the field in question. I used alerts to make sure the else block is being fired, so I know that's not it.

Code:

$('#WindowOpen').click(function (event) {
  event.preventDefault();
  $('#forgot_pw').slideToggle(600);

  if('#forgot_pw') {
    $('#login_uname, #login_pass').attr('disabled','disabled');
  } else {
    $('#login_uname, #login_pass').removeAttr('disabled');
  }
});

Thanks.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Tarek
  • 759
  • 2
  • 8
  • 17
  • 1
    if('#forgot_pw') will always run. I'm guessing this is a typo? – mtyaka Nov 23 '09 at 18:40
  • 2
    For future reference, jQuery recommends using the `.prop()` method (http://api.jquery.com/prop/) when getting and setting input properties like disabled. It's more cross browser friendly depending on the version of jQuery you use and what browser your users navigate with. – jasonmerino Mar 09 '12 at 17:36
  • 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:01

3 Answers3

34

All good used this:

$('#WindowOpen').toggle(
    function()
    {
        $('#login_uname, #login_pass').attr("disabled","disabled");     
    },
    function()
    {
        $('#login_uname, #login_pass').removeAttr("disabled");      
    });
jball
  • 24,791
  • 9
  • 70
  • 92
Tarek
  • 759
  • 2
  • 8
  • 17
7

Your problem is that the following line of code will always evaluate to true.

if('#forgot_pw')

try replacing with

if($('#forgot_pw').attr('disabled'))
Blake Taylor
  • 9,217
  • 5
  • 38
  • 41
4
$('#forgot_pw').attr('disabled', false);

should work for you.

Dmitry Polushkin
  • 3,283
  • 1
  • 38
  • 44