0

Hi this is my check boxes

<table cellpadding="0" cellspacing="0" border="1" bordercolor="red" width="100%" >
<tr><td width="50%"><input type="checkbox" name="businessTypeGroup" id="chkAll" value="0" >All</TD><TD><input type="checkbox" name="businessTypeGroup" id="chkBuyer" value="1">Buyer/Importer</td></tr>
<tr><td><input type="checkbox" name="businessTypeGroup" id="chkSeller" value="2">Seller/Exporter/Manufacturer</TD><TD><input type="checkbox" name="businessTypeGroup" id="chkService" value="3">Service Provider</td></tr>                          
<tr><td><input type="checkbox" name="businessTypeGroup" id="chkDistributor" value="4">Distributor</td><td><input type="checkbox" name="businessTypeGroup" id="chkSupplier" value="5">Supplier</TD></tr>
<tr><td colspan="2"><input type="checkbox" name="businessTypeGroup" id="chkTrading" value="6">Trading Company&nbsp;<span id="businessTypeGroupError"></td></tr>
</table>

As per this code one senario is not working 1] try to check other checkboxes not able to check All

$("input:checkbox[name='businessTypeGroup']").click (function(){
$("input:checkbox[name='businessTypeGroup']").click (function(){
var totalCheckboxes = $("input:checkbox[name='businessTypeGroup']").length;         
var checkedCheckboxes = $("input:checkbox[name='businessTypeGroup']:checked").length;
    if ( totalCheckboxes === checkedCheckboxes ){               
        $("#chkAll").attr("checked" , true );   
    }else{
        $("#chkAll").attr("checked" , false );
    }   
   });
 });

 $("#chkAll").click ( function(){
   $("input:checkbox[name='businessTypeGroup']").attr ( "checked" , $(this).attr("checked") );  
});
2
  • you are having some typo - closing brace missing in the click-assignment Commented Jan 12, 2010 at 10:46
  • It is because I have altered your HTML. Commented Jan 12, 2010 at 12:40

1 Answer 1

11

Use ids for the checkboxes and then use the above method since # is an id selector.

or you can use

$(function(){
    $("input:checkbox[name='businessTypeGroup']").click (function(){
        alert("test");
    });
});

See attributeEquals selector

Edited

$(function(){
    $("input:checkbox[name='businessTypeGroup']").click (function(){
        $("#chkAll").attr ( "checked" , false );
});

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

<input type="checkbox" id="chkAll" value="0" >All
<input type="checkbox" id="chkBuyer" name="businessTypeGroup"  value="1" >Buyer/Importer
<input type="checkbox" id="chkSp" name="businessTypeGroup"  value="2" >Service Provider
<input type="checkbox" id="chkSupp" name="businessTypeGroup"  value="3" >Supplier

Working Demo

Another demo

This will check the checkbox All when all other checkboxes are checked. And if any one is unchecked then checkbox All will be unchecked.

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

4 Comments

Remember to leave the name attribute so that the values is posted. (I am not complete sure, that this is necessary, but it's certainly a practice to always specify the name of an input type="checkbox".)
Still there is problem when i try to uncheck the All checkbox. its not working properly
My actual requirement is 1] When i check All it will check all check boxes. 2] When i uncheck All it will uncheck all check boxes. 3] When All is checked and uncheck any other it will uncheck All check box.
In Demo when i try to check below check box it should check All also Buyer/Importer Service Provider Supplier

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.