How to enable a disabled checkbox dynamically?

batman

Please see here: http://jsfiddle.net/nShQs/

Press the disable button and then the enable button. The checkbox doesn't get enabled.

HTML:

<input id="check" type="checkbox"/>
<input id="btn1" type="button" value="enable" />
<input id="btn2" type="button" value="disable" />

JS:

function enable() {
    var x = document.getElementById("check");
    alert(x.getAttribute("disabled"));
    x.setAttribute("disabled", "false");
    alert(x.getAttribute("disabled"));
}

function disable() {
    var x = document.getElementById("check");
    alert(x.getAttribute("disabled"));
    x.setAttribute("disabled", "true");
    alert(x.getAttribute("disabled"));
}
document.getElementById("btn1").addEventListener("click", enable);
document.getElementById("btn2").addEventListener("click", disable);

answer

As the answers tell it is because the disabled attribute is a boolean attribute. See here.

PSL

Just do

function enable() {
    document.getElementById("check").disabled= false;

}

function disable() {
     document.getElementById("check").disabled= true;
}

With this you are setting the property of the DOM element, while setting attribute presence of attribute disabled will disable the check box, so even if you do x.setAttribute("disabled", "false"); it will still be there on the element as attribute.

Demo

or you would just do:

function disable() {
    document.getElementById("check").setAttribute('disabled', 'disabled');
}

function enable() {
   document.getElementById("check").removeAttribute('disabled');
}

disabled as attribute and disabled as property are different.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to dynamically enable a disabled ion-tab?

From Dev

How to dynamically link a CheckBox to enable a TextBox in C# (WPF)?

From Dev

how to put checkbox in disabled?

From Dev

Enable a <span> nested in a disabled jQuery mobile Checkbox

From Dev

How to enable/disable a checkbox

From Dev

Disabled Cortana, how to enable it now?

From Dev

I need to enable form (it is disabled on page load) after checkbox checked

From Dev

how to dynamically enable and disable button when checkbox is checked and unchecked in semantic UI

From Dev

how to dynamically enable and disable button when checkbox is checked and unchecked in semantic UI

From Dev

Dynamically disabled some checkbox options using jquery and php

From Dev

How to enable text wrapping in CheckBox

From Dev

How to enable an input by clicking a checkbox

From Dev

How to enable NonChecked checkbox in WPF

From Dev

How to enable checkbox when checked?

From Dev

how to enable a disabled event handler with jquery

From Dev

How to enable one button inside a disabled fieldset

From Dev

How to enable copy paste on disabled TextBox

From Dev

How to enable tooltip on disabled button in javascript?

From Dev

How to enable tooltip on disabled button in javascript?

From Dev

How to enable all disabled when print

From Dev

How to enable copy paste on disabled TextBox

From Dev

How to enable the disabled Streaming Processors (SM)?

From Dev

how to enable a disabled event handler with jquery

From Dev

My super key is disabled. How to enable it?

From Dev

How to enable a currently disabled button in Swift

From Dev

Why is ‘Archive…’ disabled in Evolution (or how to enable it)

From Dev

how to make checkbox disabled angular.js

From Dev

How to enable CheckBoxField in GridView dynamically?

From Dev

How to disable and enable div dynamically

Related Related

  1. 1

    how to dynamically enable a disabled ion-tab?

  2. 2

    How to dynamically link a CheckBox to enable a TextBox in C# (WPF)?

  3. 3

    how to put checkbox in disabled?

  4. 4

    Enable a <span> nested in a disabled jQuery mobile Checkbox

  5. 5

    How to enable/disable a checkbox

  6. 6

    Disabled Cortana, how to enable it now?

  7. 7

    I need to enable form (it is disabled on page load) after checkbox checked

  8. 8

    how to dynamically enable and disable button when checkbox is checked and unchecked in semantic UI

  9. 9

    how to dynamically enable and disable button when checkbox is checked and unchecked in semantic UI

  10. 10

    Dynamically disabled some checkbox options using jquery and php

  11. 11

    How to enable text wrapping in CheckBox

  12. 12

    How to enable an input by clicking a checkbox

  13. 13

    How to enable NonChecked checkbox in WPF

  14. 14

    How to enable checkbox when checked?

  15. 15

    how to enable a disabled event handler with jquery

  16. 16

    How to enable one button inside a disabled fieldset

  17. 17

    How to enable copy paste on disabled TextBox

  18. 18

    How to enable tooltip on disabled button in javascript?

  19. 19

    How to enable tooltip on disabled button in javascript?

  20. 20

    How to enable all disabled when print

  21. 21

    How to enable copy paste on disabled TextBox

  22. 22

    How to enable the disabled Streaming Processors (SM)?

  23. 23

    how to enable a disabled event handler with jquery

  24. 24

    My super key is disabled. How to enable it?

  25. 25

    How to enable a currently disabled button in Swift

  26. 26

    Why is ‘Archive…’ disabled in Evolution (or how to enable it)

  27. 27

    how to make checkbox disabled angular.js

  28. 28

    How to enable CheckBoxField in GridView dynamically?

  29. 29

    How to disable and enable div dynamically

HotTag

Archive