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 3931204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:19:47+00:00 2026-05-19T23:19:47+00:00

I have a country list. It’s splitted up 3 times. [ ] World [

  • 0

I have a country list. It’s splitted up 3 times.

[ ] World
    [ ] Africa
        [ ] Congo
    [ ] Europe
        [ ] Netherlands, the
        [ ] Italy

now if I click world, I want all of them to be checked.
If I click Africa, I only want Congo to be checked.
If I click Europe, Both Italy and Netherlands, the should be checked.
If I click Netherlands, the, and Italy, Europe should be checked, and the same should happen with World when clicking Europe and Africa.
I came up with the following jquery script for it:

$(function() {
    $(":checkbox").click(function() {
        var maxlevel = 3;
        var level = $(this).attr("class").substring(5, 6);
        var level_checked = $(this).attr("checked");
        //update children
        $('input', $(this).parent('div')).each(function() {
            var curlevel = $(this).attr("class").substring(5, 6);
            if (curlevel > level) {
                $(this).attr("checked", level_checked);
            }
        });
        //update parents?
    });
});

I am using the following html code:

<div>
    <input class="layer1" type="checkbox" />Layer1 <br />
    <div>
        <input class="layer2" type="checkbox" />Layer2 <br />
        <div>
            <input class="layer3" type="checkbox" />Layer3 <br />
            <input class="layer3" type="checkbox" />Layer3 <br />
        </div>
    </div>
    <div>
        <input class="layer2" type="checkbox" />Layer2 <br />
        <div>
            <input class="layer3" type="checkbox" />Layer3 <br />
            <input class="layer3" type="checkbox" />Layer3 <br />
        </div>
    </div>
</div>

Updating the children when clicking something works, but updating the parents when clicking a box.. I have no idea how to get that done. I found 2 pieces of code that did this, but they only worked for 1 ‘subgroup’ of checkboxes, and not for 2 like in my case.

Can anyone help me out here?

  • 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-05-19T23:19:47+00:00Added an answer on May 19, 2026 at 11:19 pm

    Welcome to algorithms class. Here’s the basic format:

    Write the question down, as something which can be answered. This is step 1.

    Step 2, write the answer down as a series of steps, and make each step as detailed as reasonable.

    Step 3, convert each specific step into either code or pseudocode (repeating step 3 to convert pseudocode to code).

    Congratulations, you’ve now implemented an algorithm in code. So for your specific question, I’ll do the first two steps, and let you do the third:

    How do I check all of the parent checkboxes of a given checkbox when it gets clicked?

    sidenote: this is a recursive function. Any checkbox which gets called may need this called on it, but only for non-sibling checks. So you have two routes to consider. I’ll consider the sibling route.

    consider also that the parent of a child element may be semi-checkable or only support on/off states. I’ll call that SEMI in my notes. You will choose one path based on if SEMI is in effect.

    • A checkbox is activated.
      • There are two states: The checkbox could now be unchecked, or it could now be checked.
    • If the checkbox is now unchecked
      • (SEMI) check to see if any sibling elements are checked, and if so, mark the parent as semi-checked OR
      • uncheck the parent if that semi-check state is not supported.
    • If the checkbox is now checked
      • (SEMI) check to see if any sibling elements are unchecked, and if so, mark the parent as semi-checked OR
      • check to see if all sibling elements are checked
      • if all siblings are checked, mark parent as checked
      • else mark parent as unchecked
    • call this operation on the element’s parent element, if the element is not the root element

    How do I check all of the sibling checkboxes to determine their state?

    • obtain a list of all the sibling elements for a given element
    • define a variable for checked, and a variable for unchecked, and a variable for the total number of elements (counter)
    • visit each checkbox in the sibling group and increment the appropriate counter for checked or unchecked, and increment the counter variable regardless of which other one you increment.
    • If determining if all the siblings are unchecked, compare the counter to the unchecked
    • If determining if any siblings are unchecked, compare the unchecked value to 0 (greater than zero means at least one is unchecked)
    • If determining if all the siblings are checked, compare the counter to the checked
    • If determining if any siblings are checked, compare the checked value to 0 (greater than zero means at least one is checked)

    Note that there are faster/easier inline methods for determining the answer to #2, but depending on how you abstract it, that is an easy method to implement and only requires upkeep in one place.


    I don’t doubt that when you posted this question you were hoping for an easy answer, and while there are some shortcuts, this is the simplest way (and simplest is always most maintainable) so do it the simplest way for now. When you have specific code that you want to optimize that can be done later.

    If you do as I helped you here, and write the long code, you’ll be able to accomplish what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html drop down list,which has country list. Now I want to set
I want to ask how to add country list dropdown in cakephp? I have
I have a list called Countries, and each country has a list of Towns,
I have this drop-down list of countries: <select name=country> <option value=>Country...</option> <option value=Afganistan>Afghanistan</option> <option
I need a city/state/country Dynamic Drop-Down List in my Wordpress registration page. I have
I have a working country map that with regions that animates fill on click
I have list of country names or other strings in a text blob field.
I have a list: Name Country Value John Ireland 100 Mary UK 200 Peter
I have a list of countries, html, they have numeric ids and country name:
i have a txt file with a list of country's. For my form i

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.