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

  • Home
  • SEARCH
  • 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 498409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:52:35+00:00 2026-05-13T05:52:35+00:00

I have the following code. I need help fixing it such that the Category

  • 0

I have the following code. I need help fixing it such that the “Category” checkbox for each category should be checked only if all the items under that are checked.

<html>
<head>

<script src="http://www.google.com/jsapi"></script>

<script>
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
</script>

<script language="JavaScript">
function toggleTableRows()
{
  $(document).ready(function() {
    $('img.parent')
      .css("cursor","pointer")
      .toggle(
        function() {
          $(this).attr("title","Click to Collapse")
          $(this).attr("src","arrow_expanded.gif");
          $('tr').siblings('#child-'+this.id).toggle();
        },
        function() {
          $(this).attr("title","Click to Expand");
          $(this).attr("src","arrow_collapsed.gif");
          $('tr').siblings('#child-'+this.id).toggle();
        }
      );

    initCheckBoxes();
  });
}

function toggleCheckboxes(current, form, field) {
  $(document).ready(function() {
    $("#"+ form +" :checkbox[name^='"+ field +"[']")
      .attr("checked", current.checked);
  });
}

function toggleParentCheckboxes(current, form) {        
  var checked = $("#"+ form +" :checkbox[name='"+ current.name +"']").length == 
                $("#"+ form +" :checkbox[name='"+ current.name +"']:checked").length;
  // replace '[anything]' with '' instead of just '[]'
  $("#"+ form +" :checkbox[name='"+ current.name.replace(/\[[^\]]*\]/, "") +"']")
    .attr("checked", checked);
}

function initCheckBoxes(form) {
  $("#"+ form +" :checkbox:checked").each(function() {
    if (this.name.match(/chk[0-9]\[.*\]/)) {
      toggleParentCheckboxes(this, form);
    }
  });
}
</script>
<script language="JavaScript">toggleTableRows();</script>
</head>
<body>

<form name="frmDinnerMenu" id="frmDinnerMenu" method="POST" action="">
<table border=1>
  <tr>
    <td><img class="parent" id="0" src="arrow_collapsed.gif"
          title="Click to Expand">Category - Fruits</td>
    <td><input type="checkbox" name="chk0"
          onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk0');"/></td>
  </tr>
  <tr style="display: none;" id="child-0">
    <td>Apple</td>
    <td><input type="checkbox" value="0" name="chk0[1]"
          onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
  </tr>
  <tr style="display: none;" id="child-0">
    <td>Banana</td>
    <td><input type="checkbox" checked value="0" name="chk0[2]"
          onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
  </tr>
  <tr style="display: none;" id="child-0">
    <td>Orange</td>
    <td><input type="checkbox" checked value="0" name="chk0[5]"
          onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
  </tr>

  <tr>
    <td><img class="parent" id="1" src="arrow_collapsed.gif"
          title="Click to Expand">Category - Vegetables</td>
    <td><input type="checkbox" name="chk1"
          onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk1');"/></td>
  </tr>
  <tr style="display: none;" id=child-1>
    <td>Cabbage</td>
    <td><input type="checkbox" checked value="0" name="chk1[21]"
          onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
  </tr>
  <tr style="display: none;" id=child-1>
    <td>Tomatoes</td>
    <td><input type="checkbox" value="0" name="chk1[26]"
          onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
  </tr>
  <tr style="display: none;" id=child-1>
    <td>Green Peppers</td>
    <td><input type="checkbox" checked value="0" name="chk1[29]"
          onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
  </tr>
</table>
</form>
</body>
</html>
  • 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-13T05:52:35+00:00Added an answer on May 13, 2026 at 5:52 am

    your html is still a mess but here is the solution

    <!DOCTYPE html>
    <html>
    <head>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.toggle_subcheckbox').click(function(){
                $('.subcheckbox', $(this).parents('div')).toggle();
            });
    
    
            $('.subcheckbox input').change(function(){
                $this = $(this);
                var checker = true;
                $('input', $this.parents('.subcheckbox')).each(function(){
                    if( ! $(this).is(':checked')){
                        checker = false
                    }
                    else {
                        $('.maincheckbox', $this.parents('div')).attr('checked', false);
                    }
                });
    
                if(checker){
                    $('.maincheckbox', $this.parents('div')).attr('checked', true);
                }
            });
    
            $('.maincheckbox').change(function(){
                var state = $(this).is(':checked');
    
                $('.subcheckbox input', $(this).parents('div')).each(function(){
                    $(this).attr('checked', state);
                });
            });
        });
    </script>
    <style type="text/css" media="screen">
        .subcheckbox {
            display: none;
        }
    </style>
    </head>
    <body>
        <form id="frmDinnerMenu" method="post" action="">
            <div>
                <label><img class="toggle_subcheckbox" src="arrow_collapsed.gif" /> Category - Fruits</label>
                <input class="maincheckbox" type="checkbox" name="chk0" />
                <div class="subcheckbox">
                    <div>
                        <label>Apple</label>
                        <input type="checkbox" value="0" name="chk0[1]" />
                    </div>
                    <div>
                        <label>Banana</label>
                        <input type="checkbox" value="0" name="chk0[2]" />
                    </div>
                    <div>
                        <label>Orange</label>
                        <input type="checkbox" value="0" name="chk0[5]" />
                    </div>
                </div>
            </div>
            <div>
                <label><img class="toggle_subcheckbox" src="arrow_collapsed.gif" /> Category - Vegetables</label>
                <input class="maincheckbox" type="checkbox" name="chk0" />
                <div class="subcheckbox">
                    <div>
                        <label>Cabbage</label>
                        <input type="checkbox" value="0" name="chk1[21]" />
                    </div>
                    <div>
                        <label>Tomatoes</label>
                        <input type="checkbox" value="0" name="chk1[26]" />
                    </div>
                    <div>
                        <label>Green Peppers</label>
                        <input type="checkbox" value="0" name="chk1[29]" />
                    </div>
                </div>
            </div>
        </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 312k
  • Answers 312k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the comma selector: $("div, p")... or add(): $("div").add("p")... May 13, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer It's because val is not null, but contains 'null' as… May 13, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer On my linux machine it is downloaded in the temporary… May 13, 2026 at 10:34 pm

Related Questions

I need help of a real C guru to analyze a crash in my
I am the lone software engineer on a team that develops physics models (approx
This seems to be pretty straight forward. I need to send email from some
Possible Duplicate: splitting a string i have a string which looks like this: http://pastebin.com/m5508ff19
I need help converting the following code snippet to use SPSiteDataQuery instead of SPQuery

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.