Link to home
Start Free TrialLog in
Avatar of TOM BURKHARDT
TOM BURKHARDTFlag for Canada

asked on

javascript count checked checkboxes

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.
Avatar of sh0e
sh0e


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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna 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
Avatar of TOM BURKHARDT

ASKER

pretty darn cool, thanks!!
You saved me literally hours of head-banging which is time I simply cannot afford right now. Thanks so much,
you are welcome.