Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript count checked checkboxes

Posted on 2008-10-20
5
Medium Priority
?
973 Views
Last Modified: 2012-05-05
hi experts.
I have a form with two sets of checkboxes in the same form (unavoidable, client request).

I need to limit the number of boxes checked, in EACH set, to two.

The IDs of checkbox set #1 are: PR2a,PR2b,PR2c,PR2d,PR2e,PR2f,PR2g,PR2h,PR2i
The IDs of checkbox set #1 are: PR3a,PR3b,PR3c,PR3d,PR3e,PR3f,PR3g,PR3h,PR3i

I need my javascript to raise an alert
if the first set has more than two boxes checked
AND/OR
if the second set has more than two boxes checked

A separate function for each would be fine.

the ID of my form is "frm1", and my form calls the function(s) with the statement onclick="return checkit(this.form)"

Any help would be much appreciated. I am pretty good at VBScript, but a relative newbie at javascript.
0
Comment
Question by:tom_burkhardt
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22759653

function checkit(formid){
var id1 = "PR2a,PR2b,PR2c,PR2d,PR2e,PR2f,PR2g,PR2h,PR2i";
var id2 = "PR3a,PR3b,PR3c,PR3d,PR3e,PR3f,PR3g,PR3h,PR3i";
var count1=0, count2=0;
arr1 = id1.split(',');
arr2 = id2.split(',');
for(i=0;i<arr1.length;i++)
if(document.getElementById(arr1[i]).checked == 1)
count1++;
for(i=0;i<arr2.length;i++)
if(document.getElementById(arr2[i]).checked == 1)
count2++;
if(count1 > 2 || count2 > 2)
alert('more than 2 checked');
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22759709
try:
function checkit(f)
{
	var valid=true;
	var msg = "";
	var ids = [
			{"group":"PR2a,PR2b,PR2c,PR2d,PR2e,PR2f,PR2g,PR2h,PR2i","max":2,"checked":0, "label":"Group 1"}
			,{"group":"PR3a,PR3b,PR3c,PR3d,PR3e,PR3f,PR3g,PR3h,PR3i","max":2,"checked":0, "label":"Group 2"}
			]; 
	for( var i=0, limit=ids.length; i < limit ++i)
	{
		var e = ids.group.split(",");
		for( var j=0; j < e.length; ++j)
		{
			var t = document.getElementById( e[j] );
			if( t && t.checked )
			{
				++ids[i].checked;
			}
		}
		if( ids[i].checked > ids[i].max)
		{
			valid=false;
			msg += "You may choose at most " + ids[i].max+ " from " + ids[i].label +"\n";
		}
	}
	if( !valid )
	{
		alert(msg);
	}
return valid;
}

Open in new window

0
 

Author Comment

by:tom_burkhardt
ID: 22759753
pretty darn cool, thanks!!
0
 

Author Closing Comment

by:tom_burkhardt
ID: 31507889
You saved me literally hours of head-banging which is time I simply cannot afford right now. Thanks so much,
0
 
LVL 82

Expert Comment

by:hielo
ID: 22759831
you are welcome.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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

824 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