Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:23:29+00:00 2026-06-16T05:23:29+00:00

I have several groups of checkboxes. Each group is contained in a div which

  • 0

I have several groups of checkboxes. Each group is contained in a div which is inside a fieldset.

The div has the class chk_div applied to it. I want to be able to limit the number of checkboxes the user can select to 3. I have a function which does this and it works if I give each checkbox a unique ID and refer to it.

However I would like to be able to do it via the chk_div class. So I can have as many groups of checkboxes as I want and only have to do the jQuery once.

Here is the code that works with a unique id for each checkbox. – the container will be a div id.

function CheckboxCount(container,maximum)
{//Counts all the checked checkboxes in the given container and once the maximum number of boxes are checked it disables all the rest

    var Checked = ($(container +' :checkbox:checked').length); //Get the number of checkboxes in this container that are checked

    //If the maximum number of checkboxes in the given container have been checked we disable the unchecked ones until the number of checked is lower than max
    if (Checked >= maximum){$(container +' :checkbox:not(:checked)').attr("disabled",true);} //Disable all non checked check boxes
    else{$(container +' :checkbox').attr("disabled",false);} //Enable all checkboxes
}

This function is triggered by code such as

$('#group1').click(function(){CheckboxCount('#group1',3);});
$('#group2').click(function(){CheckboxCount('#group2',3);});

Where group1, group2 are the id of the divs that contain the checkboxes.

What I want is something more like this

function test(container,maximum)
{
    $(container +' :checkbox').click(function(){

    var Checked = ($(container+' :checkbox:checked').length);

    if (Checked >= maximum){$(container +' :checkbox:not(:checked)').prop("disabled",true);} 
    else{$(container +' :checkbox').prop("disabled",false);} //Enable all checkboxes}

    });
}

Where the container is a class, and as you can see the .click event handler goes inside the function. Only problem with this is that it applies to all groups no matter which group the checkbox belongs to.

So if I click three checkboxes in group one, it also disables those in group 2

Here’s the jsFiddle so you can see what I mean. – http://jsfiddle.net/jSgp9/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:23:30+00:00Added an answer on June 16, 2026 at 5:23 am

    I’d simplify it to this jsFiddle example.

    $('.chk_div input').click(function() {
        if ($(this).parents('.chk_div').find('input:checked').length >= 3) {
            $(this).parents('.chk_div').find(':checkbox:not(:checked)').prop("disabled", true);
        }
        else {
            $(this).parents('.chk_div').find(':checkbox').prop("disabled", false);
        }
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several groups of elements, each group of which is related to a
I have several checkboxes, each group with only one selectable component. An example of
I work in several groups, each of which has its own tab/indentation/spacing standards in
I have created several shared example groups for Rspec which I would like to
Ok, so i have a form and it has several groups of fields, like
I'm using an e-commerce system which allows you to have several variations for each
I am using nested repeaters. I render several groups and every group has members
Hi I have several Groups in my iPhone addressbook which contains several contacts. For
The scenario: a User class has several groups of properties: password , address ,
I have several users say: A,B,C,D, etc. and 2 group: master, slave. Each user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.