0

enter image description hereI have 4 group of checkboxes, in which i am grouping all this 4 in to an array, after i stored the group checkboxes, i can't add the event to those boxes, i think i am doing something wrong.. any one correct my code?

what i want is when a user click on a checkbox, then can check further withing group(max 4 allowed), if they switch to other group, then rest of the groups need to uncheck, and they have the rights to check 4 from which group they selected.

i am grouping like this, and next i need to disable and unable to group to allowing 4 selection, any good idea to get this done?

var geoGroup = [];

$('input:checkbox','#geography').each(function(i){
    if(typeof geoGroup[$(this).attr('name')] == 'undefined')
        geoGroup[$(this).attr('name')] = [];
    geoGroup[$(this).attr('name')].push($(this));

    $('input:checkbox',geoGroup[$(this).attr('name')]).live('click', function(){
        alert('hi')
    })
})
2
  • could you provide the associated html please ? Commented Mar 31, 2012 at 14:20
  • screen attached for reference, thanks Commented Mar 31, 2012 at 14:54

3 Answers 3

1

If you are using jQuery 1.7 use on() rather than live as live() is deprecated. Following uses no array, it simply unchecks other groups.

Assumes that each group is wrapped in a container with a class='checkgroup'

Demo: http://jsfiddle.net/dqQwB/1/

$('#geography').on('change', 'input:checkbox', function() {
    $('.checkgroup').not($(this).closest('.checkgroup')).find('input:checkbox').prop('checked', false);
});
Sign up to request clarification or add additional context in comments.

4 Comments

it works. but always i need a check group can eligible to add any other group. ok let me try that. thanks.
if you would have provided the html it would have been far easier to create demo and understand issue. The limit of 4 was not easy to understand and still not sure what rules are exactly. It is easy to count how many are checked with jQuery :checked selector
this version limits to only 4 in a group and disables others in group jsfiddle.net/dqQwB/2
yes, this is what i looking for. thanks. yes, good idea to place html, let do this in my upcoming request. very thanks to you!
0

With minor cleanup, it runs just fine. There's a minor bug in your original code (repeated in my example) in that you traverse using the context of the array item, when the array item has no children and thus doesn't need to be traversed: http://jsfiddle.net/imsky/j87Sx/1/

var geoGroup = [];
$("input:checkbox", "#geography").each(function() {
    var name = $(this).attr("name");
    if (name != null) {
        geoGroup[name] = $(this);
        geoGroup[name].click(function() {
            alert("hello");
        });
    }
});

Comments

0

You don't need an array to achieve what you want.

        $('input:checkbox', "#geography").each(function(i){
            $(this).click(function(){ 
                var name = $(this).attr('name');
                $('input:checkbox', "#geography").each(function(i){
                    if($(this).attr('name') != name){
                        $(this).attr("checked", false);
                    }
                });
            })
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.