Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable a disabled checkbox dynamically?

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.

like image 256
batman Avatar asked Jul 11 '13 17:07

batman


People also ask

How do I enable and disable a checkbox?

Press the disable button and then the enable button. The checkbox doesn't get enabled. As the answers tell it is because the disabled attribute is a boolean attribute.

How do I enable a checkbox in HTML?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.

How do you disable a checkbox when another is checked?

attr("checked")) be if ($('#incarcerated'). attr("checked") || $('#support'). attr("checked")) ? Checking both lower checkboxes, then unchecking one will enable the first checkbox again.


2 Answers

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.

like image 191
PSL Avatar answered Sep 28 '22 22:09

PSL


Set the disabled property rather than the attribute (fiddle).

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

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

A control will remain disabled if the disabled attribute is present at all - regardless of its value (fiddle). Setting the disabled property to false will remove the disabled attribute.

like image 36
canon Avatar answered Sep 28 '22 22:09

canon