Javascript Disable/Enable Checkbox

Here is a quick function for you to disable or enable checkboxes using Javascript. The functon takes on two parameters, 1st is the checkbox object while the 2nd parameter is a boolean which is used to set a checkbox if it should be enabled or disabled.

?View Code JAVASCRIPT
1
2
3
4
5
6
7
function disableCheckbox(checkbox, disable) {
  var max = checkbox.length;
  for (var i=0; i<max; i++) {
    checkbox[i].disabled = disable;
  }
 
}

To use the function,

do this disableCheckbox(document.form_name.checkbox_name, true);

This, provided that your html form tags look like this

?View Code HTML4STRICT
1
2
3
4
<form>
  <input name="checkbox_name" type="checkbox" value="1" /> 1st checkbox
  <input name="checkbox_name" type="checkbox" value="2" /> 2nd checkbox
</form>

I tried not using form tags and relied on the id attribute for the checkboxes but it seemed that it does not work. So, that is one tip for you. Hope anyone finds this useful.

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails

3 Responses to “Javascript Disable/Enable Checkbox”

  1. 1
    User links about "checkbox" on iLinkShare Says:

    [...] | user-saved public links | iLinkShare 4 votesJavascript: Disable/Enable Checkbox>> saved by rslpemilyrox 1 days ago3 votesAmerican Airlines, AA.com and "developing [...]

  2. 2
    Recent Links Tagged With "checkbox" - JabberTags Says:

    [...] on Sun 01-2-2009 The Professional’s Guide to MySpace Saved by imaginarium on Fri 30-1-2009 Javascript: Disable/Enable Checkbox Saved by pipka on Fri 30-1-2009 EXERCISE 9.5 Saved by dustandmold on Tue 27-1-2009 Ubuntu [...]

  3. 3
    Mattias Says:

    Didn’t work for me as is. But this small change worked for me.

    checkbox[i].disabled = true;

Leave a Reply

Spam protection by WP Captcha-Free