Abiel de Groot
asked on
Check all checkboxes ... not working perfectly
The below code works ... but not all that well. If no checkboxes are checked it will check them all. However, if at least one is checked it will not check the others.
Any ideas?
<!-- Begin
function isChecked(chks)
{
var rval = false;
for(var i=0; i<chks.length; i++)
{
if(chks[i].checked)
{
rval = true;
break;
}
}
return rval;
}
function SelAll(field){
if (!isChecked(document.formPageEditor["Chk_IncYr"]))
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
document.getElementById("AllChecked").checked = true;
}
// End -->
Any ideas?
Can you include any of the checkbox HTML as an example and perhaps the HTML for the checkbox used to check all the checkboxes.
Going on what you've posted so far though, the isChecked function returns true as soon as it finds one of the checkboxes (in document.formPageEditor["C hk_IncYr"] ) is checked, in the SellAll function it only goes through all the checkboxes (in field) and checks them if isChecked returns false. So therefore if any checkbox is checked it will never check all the checkboxes. But I can't imagine what's in field or document.formPageEditor["C hk_IncYr"] without the HTML.
Going on what you've posted so far though, the isChecked function returns true as soon as it finds one of the checkboxes (in document.formPageEditor["C
That is correct - but what do you want it to do?
function SelAll(field){
// if none are checked then proceed and set the others.
// if one is checked isChecked returns true so the for loop
// won't run
if (!isChecked(document.formPageEditor["Chk_IncYr"]))
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
ASKER
The HTML just lists the options. The checkboxes are dynamically generated and look like this
<input name="Chk_IncYr" type="checkbox" id="Chk_IncYr1" value="1" />
<input name="Chk_IncYr" type="checkbox" id="Chk_IncYr1" value="2" />
<input name="Chk_IncYr" type="checkbox" id="Chk_IncYr1" value="3" />
Obviously they have labels ..
The call looks like this.
A:
<input name="Chk_IncYr" type="checkbox" id="Chk_IncYr1" value="1" />
<input name="Chk_IncYr" type="checkbox" id="Chk_IncYr1" value="2" />
<input name="Chk_IncYr" type="checkbox" id="Chk_IncYr1" value="3" />
Obviously they have labels ..
The call looks like this.
A:
ASKER
PS. The ID's are unique on all ...
Ok but what is the behaviour you want ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks
for (i = 0; i < field.length; i++) {
Cd&