Check All checkboxes javascript help
Posted: Wed Mar 15, 2006 8:18 pm
I finished a complex form which dynamically creates itself based on answers to previous questions. Part of the functionality of this form is that all check boxes are checked by default. I have the following Javascript code which checks all checkboxes in a particular checkbox group.
The problem happens when I view the form in Internet Exporer. I have an onload even which fires the select_all function for each group of checkboxes. It works fine in Firefox but in Internet Explorer I get the error: forminputs.length is null or not an object. Even with this error, clicking on the Check All box will select all the boxes, but it should have already been done.
The checkbox are in array format. i.e. <input type="checkbox" name="name[]" />
I don't know Javascript at all so if someone could tell me what is wrong and how to fix it I would appreciate it.
Code: Select all
var formblock;
var forminputs;
function prepare() {
formblock= document.getElementById('FormName');
forminputs = formblock.getElementsByTagName('input');
}
function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
var regex = new RegExp(name, "i");
if (regex.test(forminputs[i].getAttribute('name'))) {
if (value == '1') {
forminputs[i].checked = true;
} else {
forminputs[i].checked = false;
}
}
}
}
if (window.addEventListener) {
window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
window.attachEvent("onload", prepare)
} else if (document.getElementById) {
window.onload = prepare;
}The checkbox are in array format. i.e. <input type="checkbox" name="name[]" />
I don't know Javascript at all so if someone could tell me what is wrong and how to fix it I would appreciate it.