0

All One Two three

My requirement is

  1. when check All it should check One , Two, Three also.
  2. When i uncheck All it should uncheck One , Two, Three also
  3. When all check boxes check and uncheck either one , two , three in that case "All" check box should uncheck.
  4. check / uncheck should ne toggling

Please help

2

3 Answers 3

2
$(function(){
    $("#all").click(function(){
        $("input:checkbox[name='checkGroup']").attr("checked",$(this).attr("checked"));
    });

    $("input:checkbox[name='checkGroup']:not('#all')").click ( function(){
        var totalCheckboxes = $("input:checkbox[name='checkGroup']:not('#all')").length;
        var checkedCheckboxes = $("input:checkbox[name='checkGroup']:not('#all'):checked").length;

        if ( totalCheckboxes === checkedCheckboxes )
        {
            $("#all").attr("checked" , true );
        }
        else
        {
            $("#all").attr("checked" , false );
        }
    });
});

Demo

Sign up to request clarification or add additional context in comments.

Comments

0

What is your trying?

Let's try

<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script language="Javascript" type="text/javascript">
$(document).ready(function() {
   $("#toggle5").click(function(event){
      $('input[@type=checkbox]').each( function() {
        if($(this).val()==5)
          this.checked = !this.checked;
      });
   });
});
</script>

<a href="#" id="toggle5">Toggle 5ve</a>

<form method="post" action="" style="margin:0px">
<input type="checkbox" name="a" value="1" id="" />
<input type="checkbox" name="b" value="2" id="" />
<input type="checkbox" name="c" value="3" id="" />
<input type="checkbox" name="d" value="4" id="" />
<input type="checkbox" name="e" value="5" id="" />
<input type="checkbox" name="f" value="5" id="" />
</form> 

Comments

0

This solution uses a cb class selector on the "child" textboxes. Another solution would be to select all those that don't have an id=all. It compares the length of the set of checked child checkboxes with the length of the set of all child checkboxes to determine if all should be checked.

<script type="text/javascript">
    $(document).ready(function() {
        $('#all').click(function() {
            $('.cb').attr('checked', this.checked);
        });
        $('.cb').click(function() {
            var allChecked = $('.cb:checked').length == $('.cb').length;
            $('#all').attr('checked', allChecked);
        });
    });
</script>

<input type="checkbox" name="checkGroup" id="all" value="0" />All
<input type="checkbox" name="checkGroup" id="one" class="cb" value="1" />One
<input type="checkbox" name="checkGroup" id="two" class="cb" value="2" />Two
<input type="checkbox" name="checkGroup" id="three" class="cb" value="3" />Three

1 Comment

Now that I've read the other responses, I see this is very similar to pulse's answer.

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.