Vicky
Vicky

Reputation: 9585

Jquery checkbox problem

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") );  
});

Upvotes: 0

Views: 1022

Answers (1)

rahul
rahul

Reputation: 187010

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.

Upvotes: 11

Related Questions