Javascript Disable/Enable Checkbox
Posted by tech on
August 10, 2008
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.
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
<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.
Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.





