Solved

javascript count checked checkboxes

Posted on 2008-10-20
5
957 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
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

636 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