I need some help with a modification to a "Select All" JS. First off I have a unique situation where I will eventually have many checkboxes, and some need to remain checked even when the "Unselect All" button is used. I can't use a "reset" because that would then remove other form items already filled in from the form. I really can't set a cookie to capture the default values because where I have to use this page will not allow cookies (even sessions) to be saved, the system is locked down pretty tight.
I was thinking of (if it is possible) to be able to identify the checkboxes that I always want checked with a specific ID and therefore be able to then parse thru JS to always keep these checked.
Below is what I have so far, but I am not able to get the "ID" value, and therefore am not able to check using JS.
Any help would be greatly appreciated.
Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
// Function to "Select All" Checkboxes
var checkflag = "false";
function check(field) {
var alwaysTrue = (field.length -1)
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Restore Defaults";
}
else {
for (i = 0; i < field.length; i++) {
var checkDefault = document.getElementById(i);
document.write("" + checkDefault);
// Several Fields should always be checked (even when all others are unselected)
if (alwaysTrue == "defaultCheck") {
field[i].checked = true;
}
else {
field[i].checked = false;
}
}
checkflag = "false";
return "Check All";
}
}
// End -->
</script>
<center>
<form name=myform action="" method=post>
<b>Your Favorite Scripts & Languages</b><br>
<input type=checkbox id="checked" name=list value="1" checked>Java<br>
<input type=checkbox id="unchecked" name=list value="2">JavaScript<br>
<input type=checkbox id="unchecked" name=list value="3">ASP<br>
<input type=checkbox id="unchecked" name=list value="4">HTML<br>
<input type=checkbox id="checked" name=list value="5" checked>SQL<br>
<br>
<input type=button value="Check All" onClick="this.value=check(this.form.list)">
<br>
</form>
</center>