Ram
Ram

Reputation: 1141

how to get count of checked checkboxes from two checkbox lists using jquery?

I have two checkbox lists, i want to get the count of checked elements of these. below is my code:

<div id="divAreaListingByLocation">
  <asp:CheckBoxList ID="chklstArea" CssClass="chkarea" RepeatColumns="6" RepeatDirection="Vertical"
        runat="server" OnSelectedIndexChanged="chklstArea_SelectedIndexChanged" AutoPostBack="true">
  </asp:CheckBoxList>
</div>
<asp:Repeater ID="repRooms" runat="server" OnItemDataBound="repRooms_ItemDataBound">
    <ItemTemplate>
        <div style="height: 100%; float: none;">
            <asp:Panel ID="pnlRoomheader" runat="server" Style="width: 98%; background-color: #86ADD6;
                color: #4A4A4A; height: 20px; margin-top: 10px; text-align: left; padding: 5px;
                font-size: 15px; font-weight: bold;">
                <asp:Label ID="lblAreaName" runat="server"></asp:Label>
                <asp:Label ID="lblAreaId" Style="display: none;" runat="server"></asp:Label>
            </asp:Panel>
            <div id="divRoomListingByLocation" style="padding-bottom: 10px; padding-top: 10px;">
                <asp:CheckBoxList ID="chkRoomList" CssClass="chkRooms" RepeatColumns="6" RepeatDirection="Vertical"
                    runat="server">
                </asp:CheckBoxList>
                <asp:Label ID="lblRoomMessage" Text="This Area does not have any room." ForeColor="Red"
                    runat="server"></asp:Label>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

What i want to do is: if user not checked any of the check box from these two then it will prompt a alert to say check one of the checkbox from both lists on the click of a button.

I have tried to it with class but the class is append to the table render in html of checkboxlist.

Upvotes: 1

Views: 10507

Answers (4)

Anshuman Jasrotia
Anshuman Jasrotia

Reputation: 3185

Since you are using checkbox list it will apply the specified class to the Table not the checkbox. There you will have to use

$(".chkarea").find("input:checked").length;

This will return the count of all the checkboxes that are checked for a checkboxlist with class "chkarea"

Upvotes: 4

Adil
Adil

Reputation: 148140

You can use wild card for checkbox list id as the ids generated by checkbox list will start by this id.

count = $("[id^=chklstArea] , [id^=chkRoomList]").filter(function(){
            if($(this).is(':checked')) 
                  return $(this);
         }).length;

Upvotes: 1

Cerbrus
Cerbrus

Reputation: 72867

With jQuery:

var n = $("input:checked").length;

Assuming the asp page returns a bunch of <input> elements.

See this fiddle for a example.

Upvotes: 4

polin
polin

Reputation: 2845

    <script type="text/javascript">
function fnc()
{
    x=document.getElementById("chkdiv").elements;

    for(i=0;i<x.length;++i)
    {if(x[i].type=='checkbox' && x[i].checked)
    alert("checked");

    }
}
</script>
    <form id="chkdiv">
<input type="checkbox" id="chk1">
<input type="checkbox" id="chk2">
<button id="button" onClick="fnc()">click</button>
</form>

Upvotes: 1

Related Questions