Count the no of selected checkbox in JavaScript

Category: ASP.NETJAVA SCRIPT Comments: No comments

<script type=”text/javascript”>
function verify_selected(checkbox) {
var inputElems = document.getElementsByTagName(“input”), count = 0;
for (var i = 0; i < inputElems.length; i++) {
if (inputElems[i].type == “checkbox” && inputElems[i].checked == true) {
count++;
}
}
document.getElementById(“lblCount”).innerHTML = “You have selected <b> ” + count + ” </b> offers”;
}
</script>

<div id=”page”>
<div>
<asp:CheckBox ID=”cbOne” runat=”server” onClick=”verify_selected(‘cbOne’);” />
<asp:ImageButton ID=”imgOne” runat=”server” AlternateText=”Offer 1″ />
<asp:CheckBox ID=”cbTwo” runat=”server” onClick=”verify_selected(‘cbTwo’);” />
<asp:ImageButton ID=”imgTwo” runat=”server” AlternateText=”Offer 2″ />
<asp:CheckBox ID=”cbThree” runat=”server” onClick=”verify_selected(‘cbThree’);” />
<asp:ImageButton ID=”imgThree” runat=”server” AlternateText=”Offer 3″ />
</div>
<div>
<asp:CheckBox ID=”cbFour” runat=”server” onClick=”verify_selected(‘cbFour’);” />
<asp:ImageButton ID=”imgFour” runat=”server” AlternateText=”Offer 4″ />
<asp:CheckBox ID=”cbFive” runat=”server” onClick=”verify_selected(‘cbFive’);” />
<asp:ImageButton ID=”imgFive” runat=”server” AlternateText=”Offer 5″ />
<asp:CheckBox ID=”cbSix” runat=”server” onClick=”verify_selected(‘cbSix’);” />
<asp:ImageButton ID=”imgSix” runat=”server” AlternateText=”Offer 6″ />
</div>
<div>
<asp:Label ID=”lblCount” runat=”server”></asp:Label>
</div>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

HTML tags are not allowed.