Reputation: 1953
i am using asp.net c# jquery vs 2008
I have two check box list (cbList,checkboxList1) and one check box (chkALL). when i checked the check box(chkALL),it should check all checkboxes in Check box list(CbList)
I am using the following code in jquery.It is not working properly.It checks both checkbox list.I need to check only one checkboxlist. Any help appreciated
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
function jqCheckAll2(id, name) {
$("INPUT[@Name=" + name + "][type='checkbox']").attr('checked', $('#' + id).is(':checked'));
}
</script>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="chkAll" runat="server" Text="Check All" onclick="jqCheckAll2( this.id, 'cbList' )"/><br />
<asp:CheckBoxList ID="cbList" runat="server">
</asp:CheckBoxList>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</div>
</form>
Upvotes: 0
Views: 1114
Reputation: 264
Use the checkbox to check all or uncheck all for all the active checkbox inside a div. ClientIDMode="Static" just doesn't allow the ID change when running on server.
<asp:CheckBox ID="chkAll" runat="server" ClientIDMode="Static"/>
<asp:Label ID="lblAll" runat="server" Text="Label" ClientIDMode="Static">Select All</asp:Label>
$(document).ready(function() {
$("#chkAll").on('change', function() { // on change of state
if (this.checked) // if changed state is "CHECKED"
{
$("#lblAll").text("Unselect All");
$("#myDiv input[type=checkbox]").each(
function() {
if($(this).attr('disabled')==null)
$(this).prop('checked', true);
}
);
} else {
$("#lblAll").text("Select All");
$("#myDiv input[type=checkbox]").each(
function () {
if ($(this).attr('disabled') == null)
$(this).prop('checked', false);
}
);
}
});
});
Upvotes: 0
Reputation: 13496
I can not see exactly what is causing your problem, but what I can see is that you are using cbList
as the name which is a server side name. Instead you should use cbList.ClientID
which is a unique client side name for the control. In other words add the onclick
event side like this:
chkAll.Attributes["onclick"] = string.Format("jqCheckAll2(this.id, '{0}')", cbList.ClientID);
then change jqCheckAll2 to this:
function jqCheckAll2(id, name) {
$("#" + name).find(":checkbox").attr('checked', $('#' + id).is(':checked'));
}
Upvotes: 1