0

I am able to successfully disable a button with below syntax .

$("#list *").attr("disabled", "disabled").off('click');

However I also need to enable the button at the end of my function.Any pls help me here how to do this.

4
  • refer to namespacing for using on and off events, plus why do you need off() when you have disabled attribute? It's not necessary... Commented Sep 7, 2014 at 16:52
  • 2
    Remove the .off("click") so just the attr("disabled", "disabled") is there. To re-enable do removeAttr("disabled"). That's a horrible selector btw, do something like $("#list button") Commented Sep 7, 2014 at 16:53
  • $(..).prop('disabled', false) Commented Sep 7, 2014 at 16:54
  • You should provide a JS fiddle. People have answered your question correctly, but there's an issue outside of what's shown to us. We can help you if you post a fiddle. Commented Sep 7, 2014 at 22:10

3 Answers 3

3

Don't use the attribute to disable the field. Use the property of the DOM element. To disable:

$("#list *").prop("disabled", true);

To re-enable:

$("#list *").prop("disabled", false);
Sign up to request clarification or add additional context in comments.

1 Comment

Probably OP has to rebind the click handler as well :D
1

The .off() method removes event handlers that were attached with .on()

If you remove the event handler while you are disabling the button, even if you enable it later, it doesn't have the event handler anymore so clicking it won't trigger whatever functionality it had previously. Else you'll have to rebind the event handler as well after enabling it.

Simply disabling the button will prevent it from working, You don't have to remove the click handler at all.

You can disable the button using $("#list *").prop("disabled", true); and enable it using $("#list *").prop("disabled", false); as wolfgang Stengel mentioned in his answer

Comments

0

Try removing the disable property completely.

$('#list').removeAttr('disabled');

Demo fiddle

Update
One more thing, setting disabled to true,false,undefined etc doesn't do anything. You can check this by simply adding a disable attribute to an element(without passing it anything) and will keep it disabled forever

Updated fiddle

Update 2
As mentioned above, following fiddle describes the affects of passing 'true,false,undefined` in the disable attribute.

Example

3 Comments

"setting disabled to true,false,undefined etc doesn't do anything" - totally wrong.
@TJ why do you think so? I've added a fiddle demonstrating what i meant.
The statement quoted above is wrong. check the other answers which sets disabled to true and false. BTW attr is not the preferred way to alter property values...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.