0

If I set a button's disabled attribute to be true, and I then want to enable it after a certain period of time using jQuery, why can I not then just set the disabled attribute to be false with the jQuery?

HTML:

<form>
    <input id="myButton" type="submit" disabled="true" value="Submit"/>
</form>

jQuery:

setTimeout(EnableSubmit, 3000);

function EnableSubmit()
{
    $("#myButton").attr("disabled", "false");
}

JSFiddle: http://jsfiddle.net/TV7t4/

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247

6 Answers6

4

The disabled attribute's value doesn't matter, it just needs to be set to work.

Try removing the attribute entirely like this:

$("#myButton").removeAttr("disabled");
Vlad Cazacu
  • 1,520
  • 12
  • 12
3

I always use this, which worked for me:

$("#myButton").prop('disabled', false);

Also see this: Disable/enable an input with jQuery?

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
3

Because the disabled attribute is a boolean attribute. It works by its mere presence, regardless of its value. I could write disabled="I'm a broken paperclip" and it would still work.

Try:

document.getElementById('myButton').disabled = true; // disable
document.getElementById('myButton').disabled = false; // re-enable

Vanilla JS is so much simpler...

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
3

Use .prop() and false:

setTimeout(EnableSubmit, 3000);
function EnableSubmit() {
    $("#myButton").prop("disabled", false);
}

jsFiddle example

From the docs: The .prop() method should be used to set disabled and checked instead of the .attr() method.

j08691
  • 204,283
  • 31
  • 260
  • 272
1

You need to set it to the boolean value false, not the string "false", so lose the quotes.

$("#myButton").prop("disabled", false);

}

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Don't forget `.prop()` instead of `.attr()`. From the docs: The .prop() method should be used to set disabled and checked instead of the .attr() method. – j08691 Aug 04 '14 at 16:48
1
$("#myButton").removeAttr('disabled');
MH2K9
  • 11,951
  • 7
  • 32
  • 49