Hi all,

I need to make a function, that returns the number of checkbox groups that has any values marked.

So im not after the checkbox alone, nor their values - Lets say I have 4 groups on the page - I need to know how many groups the user has marked.

<input type="checkbox" name="groupOne" value="17" />
<input type="checkbox" name="groupOne" value="16" />
<input type="checkbox" name="groupTwo" value="acer" />
<input type="checkbox" name="groupTwo" value="dell" />
<input type="checkbox" name="groupThree" value="intel" />
<input type="checkbox" name="groupThree" value="amd" />
<input type="checkbox" name="groupFour" value="windows" />
<input type="checkbox" name="groupFour" value="mac" />

I have this, which is not dynamic - And im sure their is a smarter way - Maybe using .map (jquery), in some way?

$( '.cb' ).click( function( e ) 
    var totalCheckedGroups = 0;

    if( $( "input[name='groupOne[]']" ).is(':checked') && $( "input[name='groupTwo[]']" ).is(':checked') )
        totalCheckedGroups = 2;
        console.log( totalCheckedGroups );
    else if( $( "input[name='groupOne[]']" ).is(':checked') || $( "input[name='groupTwo[]']" ).is(':checked') )
        totalCheckedGroups = 1;
        console.log( totalCheckedGroups );

The more groups I need to check for, im getting too many scenarious to check for.

Anyone knows of a way to dynamically retreive the amount of groups holding anything from 1-xx values in JQuerian?

Best, Klemme

This made me a little curious since I have never had a reason to do this and who knows, I might have a reason for it later.

This may be useful

It goes through all of the checked checkboxes in a form and gets their names. It adds them to an array if they do not already exist. I made it a function so it is flexible and you can bind it to more than one event. There might be a better/easier way to do it, I didn't have a lot of time.

@pixelsoul like it but id parse out the "[]" for cases where your outputting id numbers so group1[1] would still match group1[2]

function getChecked(o){ var i=0, grp={};
    grp[o[i].name].push(o[i]):grp[o[i].name]=[o[i]]:0, i++; 
return grp;

invoking this function getChecked(myForm) will return you an object who's property-names are the names of groups containing currently checked elements, which can be further processed (if and as required).