javascript - How to enable a disabled checkbox dynamically? -


please see here: http://jsfiddle.net/nshqs/

press disable button , enable button. checkbox doesn't 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 answers tell because disabled attribute boolean attribute. see here.

just

function enable() {     document.getelementbyid("check").disabled= false;  }  function disable() {      document.getelementbyid("check").disabled= true; } 

with setting property of dom element, while setting attribute presence of attribute disabled disable check box, if x.setattribute("disabled", "false"); still there on element attribute.

demo

or do:

function disable() {     document.getelementbyid("check").setattribute('disabled', 'disabled'); }  function enable() {    document.getelementbyid("check").removeattribute('disabled'); } 

disabled attribute , disabled property different.


Comments