PDA

View Full Version : recheck onChange


Ultragames
04-01-2005, 04:52 AM
I have an array ( students[] ) of check boxes. Each has an id like this (Student4 or Student11)

I have another check box named For. When you click it, i use this script to uncheck all of the Student# check boxes. ( count = number of Student# check boxes)

<script type="text/javascript">
function Deselect( count ) {
for(var x=1;x<(count+1);x++){
var id = 'Student' + x;
document.getElementById(id).checked = false;
if( document.editor.For.checked == true ){
document.getElementById(id).disabled = true;
} else {
document.getElementById(id).disabled = false;
}
}
}
</script>

My question is, how can i recheck the ones that were checked before, upon unchecking the For check box.

Wow, used the work 'check' to much. Hope that makes sence, any help would be great.

glenngv
04-01-2005, 05:41 AM
Do you really want it reversed? When you check the For checkbox, the checkboxes are unchecked and when unchecked, the checkboxes are checked?
That's confusing to the user. Anyway, here's the code:

function selectDeselect( count ) {
var id;
var flag = document.editor.For.checked;
for(var x=1;x<(count+1);x++){
id = 'Student' + x;
document.getElementById(id).checked = !flag;
document.getElementById(id).disabled = flag;
}
}
If you don't want the reverse, just change:

document.getElementById(id).checked = !flag;

to:

document.getElementById(id).checked = flag;

Ultragames
04-01-2005, 05:56 AM
I think given the context of the situation, you would see why i want it to recheck the boxes that were selected.

Your code works, however not quite how i wanted. Your code rechecks all of the check boxes. I need a way to recheck only those that were previously checked.

Thus i assume there would have to be a way of holding a list of those that were previously checked, in order to go back and recheck them. This is what i couldn't do.

Thank you for your help. Hope you can add this twist to it.

glenngv
04-01-2005, 07:24 AM
Try this:
var arr = new Array();
function addCheck(oChk){
arr[oChk.id] = oChk.checked;
}

function selectDeselect( count ) {
var id;
var flag = document.editor.For.checked;
if (!flag){//check the checkboxes
for(var x=1;x<(count+1);x++){
id = 'Student' + x;
if (arr[id]){
document.getElementById(id).checked = true;
}
}
}
else{ //uncheck the checkboxes
for(var x=1;x<(count+1);x++){
id = 'Student' + x;
document.getElementById(id).checked = false;
document.getElementById(id).disabled = flag;
}
}
}

<input type="checkbox" id="Student1" onclick="addCheck(this)" />
<input type="checkbox" id="Student2" onclick="addCheck(this)" />
...

Ultragames
04-01-2005, 09:54 PM
Though i can't test it for an hour or so, that looks like it should work.

Only one hitch. The user will not nessisarily click the checkboxes. I am using PHP to precheck the boxes that should be checked based on information from the database. Is there a way your script can detect which boxes are checked on-load?

I could do an on load script, and just check the boxes and add the variables with JS, using PHP, but it would then rely on JS to function. Not sure how to explain it.

Basicly, the onClick function is still good, but i need a way to add the cartridges that i make PHP check, when the page loads.

glenngv
04-03-2005, 10:22 AM
As you loop to output checkboxes, generate a javascript that sets the hash array with corresponding id of the checked checkboxes.
<?
'PHP loop...
'determine if checkbox is checked...
'echo checkbox...
'if checkbox is checked, output this javascript
?>
<script type="text/javascript">
arr["Student<? suffix ?>"] = true;
</script>
<?
'end of if condition
...
'end loop...
?>
I'm not a PHP coder, so the syntax might be wrong. But the idea is there.