RESOLVED Selecting all checkboxes in a recordset

I have a recordset on a page listing some items, and have a checkbox for each to insert records into another table.

I have been looking at JS to be able to check / uncheck all the checkboxes, but without any luck.

The closest I got was the JS:

<script type="text/javascript">
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
if(!document.forms[FormName])
return;
var objCheckBoxes = document.forms[FormName].elements[FieldName]; 
if(!objCheckBoxes)
return;
var countCheckBoxes = objCheckBoxes.length;
if(!countCheckBoxes)
objCheckBoxes.checked = CheckValue;
else
// set the check value for all check boxes
for(var i = 0; i < countCheckBoxes; i++)
objCheckBoxes[i].checked = CheckValue;
}
</script>

Which works so long as the name of the checkbox is a constant,e.g.:

<input name="LodgeIDPL" id="LodgeIDPL_<?php echo $RepeatSelectionCounter_1; ?>" type="checkbox" value="<?php echo($row_WADAlodges['LodgeID']); ?>" />

With these as the form buttons:

<input type="button" onclick="SetAllCheckBoxes('Insert_Basic_Default', 'LodgeIDPL', true);" value="Select All Products">&nbsp;&nbsp;
<input type="button" onclick="SetAllCheckBoxes('Insert_Basic_Default', 'LodgeIDPL', false);" value="Deselect All Products">

But because the name includes an extra bit for the multiple insert function, it breaks it:

<input name="LodgeIDPL_<?php echo $RepeatSelectionCounter_1; ?>" id="LodgeIDPL_<?php echo $RepeatSelectionCounter_1; ?>" type="checkbox" value="<?php echo($row_WADAlodges['LodgeID']); ?>" />

If anyone could help out with a way to retain the counter for the multiple insert, and allow users to select / deselect all the checkboxes that would be really appreciated.

Thanks.

Just to say I managed to get this working with the following bits of code:

In the header:

<script type="text/javascript">
var check_box = new Array();
var boxes = 0;
</script>

Immediately after the checkbox:

<script type="text/javascript">
check_box[boxes] = "LodgeIDPL_<?php echo $RepeatSelectionCounter_1; ?>";
boxes += 1;		  	
</script>

Just before the closing tag:

<script type="text/javascript">
function uncheck_all(boxes, check_box){
	for(counter = 0; counter < boxes; counter++){
		document.getElementById(check_box[counter]).checked = false;
	}
}
</script>
<script type="text/javascript">
function check_all(boxes, check_box){
	for(counter = 0; counter < boxes; counter++){
		document.getElementById(check_box[counter]).checked = true;
	}
}
</script>

I am very happy to hear you solved the problem. I bet it is a lesson you will not easily forget (which is usually the case when you struggle through a problem on your own).

Thank you, @johngordon for taking the time to post the solution. This is an immense benefit to others who may face this in the future.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.