?
Solved

Check all checkboxes ... not working perfectly

Posted on 2012-08-16
8
Medium Priority
?
300 Views
Last Modified: 2012-08-16
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?
0
Comment
Question by:Abiel de Groot
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 38301167
I think you need an opening brace here:

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


Cd&
0
 
LVL 6

Expert Comment

by:Psyberion
ID: 38301172
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.
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38301187
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Abiel de Groot
ID: 38301250
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:
0
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 38301255
PS. The ID's are unique on all ...
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38301308
Ok but what is the behaviour you want ?
0
 
LVL 6

Accepted Solution

by:
Psyberion earned 2000 total points
ID: 38301309
Right, I'm not sure why you are checking to see if any are currently checked when the SelAll function is called. How about...

function SelAll(field) {
	// Get the current state of the AllChecked checkbox.
	var allChecked = document.getElementById("AllChecked").checked;
	// Set all checkboxes to the same state as the AllChecked checkbox.
	for (i=0; i<field.length; i++)
		field[i].checked = allChecked;
}

Open in new window


Then just call SelAll when the state of the AllChecked checkbox is changed (if that's what the AllChecked checkbox is for, I'm only assuming here). As far as I can tell there is no need to check the current state of the checkboxes. But if you check the AllChecked checkbox then all boxes will become checked and if you uncheck it they will all become unchecked.
0
 
LVL 5

Author Closing Comment

by:Abiel de Groot
ID: 38301354
Many thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question