faqts : Computers : Programming : Languages : JavaScript : Forms : Checkboxes

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

121 of 150 people (81%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I disable (and then enable again) a checkbox?

Apr 15th, 2000 06:39
Martin Honnen,


Both IE4+ and NN6 support
  checkBoxElement.disabled = true/false;
NN4 (and older) don't support this property but you can set the onclick 
handler to preserve the state of the checkbox:

<HTML>
<HEAD>
<STYLE>
.js {
  color: white;
  background-color: orange;
}
</STYLE>
<SCRIPT>
function preserve () { this.checked = this.storeChecked; }
function disableCheckBox (checkBox) {
  if (!checkBox.disabled) {
    checkBox.disabled = true;
    if (!document.all && !document.getElementById) {
      checkBox.storeChecked = checkBox.checked;
      checkBox.oldOnClick = checkBox.onclick;
      checkBox.onclick = preserve;
    }
  }
}
function enableCheckBox (checkBox) {
  if (checkBox.disabled) {
    checkBox.disabled = false;
    if (!document.all && !document.getElementById)
      checkBox.onclick = checkBox.oldOnClick;
  }
}
</SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript: disableCheckBox (document.formName.aCheckBox); 
void 0">
disable
</A>
|
<A HREF="javascript: enableCheckBox (document.formName.aCheckBox); void 
0">
enable
</A>
<FORM NAME="formName">
<INPUT TYPE="checkbox" CLASS="js" NAME="aCheckBox" VALUE="Kibology">
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>