Select/Deselect all items in a CheckBoxList.

vishalneeraj-24503
Posted by vishalneeraj-24503 under jQuery category on | Points: 40 | Views : 511
Suppose,we have a Checkbox as well as Checkbox list as:-
<asp:CheckBox runat="server" ID="chkAll" Text="Select All" />

<asp:checkboxlist runat="server" ID="cbList">
<asp:ListItem>Vishal</asp:ListItem>
<asp:ListItem>Neeraj</asp:ListItem>
<asp:ListItem>Sonam</asp:ListItem>
<asp:ListItem>Rutu</asp:ListItem>
<asp:ListItem>Nitin</asp:ListItem>
<asp:ListItem>Subhash</asp:ListItem>
<asp:ListItem>Amber</asp:ListItem>
</asp:checkboxlist>

Import jQuery library as:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=chkAll]").bind("click", function () {
if ($(this).is(":checked")) {
$("[id*=cbList] input").attr("checked", "checked");
} else {
$("[id*=cbList] input").removeAttr("checked");
}
});
$("[id*=cbList] input").bind("click", function () {
if ($("[id*=cbList] input:checked").length == $("[id*=cbList] input").length) {
$("[id*=chkAll]").attr("checked", "checked");
} else {
$("[id*=chkAll]").removeAttr("checked");
}
});
});
</script>

Comments or Responses

Login to post response