Link to home
Start Free TrialLog in
Avatar of Abiel de Groot
Abiel de GrootFlag for Spain

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.

<!-- 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 -->

Open in new window



Any ideas?
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

I think you need an opening brace here:

for (i = 0; i < field.length; i++) {


Cd&
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["Chk_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["Chk_IncYr"] without the HTML.
Avatar of Julian Hansen
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 ;
		
		
}

Open in new window

Avatar of Abiel de Groot

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:
PS. The ID's are unique on all ...
Ok but what is the behaviour you want ?
ASKER CERTIFIED SOLUTION
Avatar of Psyberion
Psyberion
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks