I am working with an ASP.NET repeater that displays data from a SQL server in 4 columns. One of the fields contains a checkbox, while the others are labels. There is a field called "type" which can have two values. I want to utilize JavaScript to track if a checkbox is checked and display the count for each "type" in separate textboxes. For example, one textbox should show "type 1 has 3 checked" and the other should display "type 2 has 3 checked". Currently, my JavaScript function counts all checked checkboxes and displays the total count in a textbox. How can I modify this to keep track of the count for each type based on the "type" value associated with the checked checkbox?
JavaScript
function checkrpt() {
var inputs = document.getElementsByTagName('input');
var count = 0
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox") {
if (inputs[i].checked == true)
count += 1;
}
if (count > 6) {
inputs[i].checked = false;
}
}
if (count > 0) {
document.getElementById('txtSelectionStatus').value = count + ' items have been selected';
} else {
document.getElementById('txtSelectionStatus').value = 'No items have been selected';
}
if (count > 6) {
document.getElementById('txtSelectionStatus').value = 'You can only select 6 itmes';
}
}
ASP:Repeater
<asp:repeater id="rptStep1Data" runat="server">
<HeaderTemplate>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr style="backgound-color:#DDDDDD;">
<td width="150px" align="center" class="tableheader"><b>Add To Grid</b></td>
<td width="100px" class="tableheader" align="center"><b>Distance</b></td>
<td width="400px" align="left" class="tableheader"><b>Address</b></td>
<td width="100px" align="center" class="tableheader"><b>Type</b></td>
</tr>
<tr>
<td colspan="4">
<div style="overflow:scroll; overflow-x:hidden; height: 300px; width: 100%">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
</HeaderTemplate>
<ITEMTEMPLATE>
<tr>
<td width="148px" align="center" class="content">
<asp:checkbox id="chkSelected" runat="server" onClick="javascript:checkrpt()" Checked='<%# makeChecked(Container.DataItem("Selected")) %>' />
</td>
<td width="100px" align="center" class="content">
<asp:label id="lblDistance" runat="server" Text='<%# Container.DataItem("Distance") %>' />
</td>
<td width="390px" align="left" style="padding-left:10px" class="content">
<asp:label id="lblAddress" runat="server" Text='<%# Container.DataItem("Address") %>' />
</td>
<td width="100px" align="center" class="content">
<asp:label id="lblType" runat="server" Text='<%# Container.DataItem("Type") %>' />
</td>
</tr>
</ITEMTEMPLATE>
<ALTERNATINGITEMTEMPLATE>
<tr>
<td align="center" class="content">
<asp:checkbox id="chkSelected" runat="server" onClick="javascript:checkrpt()" Checked='<%# makeChecked(Container.DataItem("Selected")) %>' />
</td>
<td align="center" class="content">
<asp:label id="lblDistance" runat="server" Text='<%# Container.DataItem("Distance") %>' />
</td>
<td align="left" style="padding-left:10px" class="content">
<asp:label id="lblAddress" runat="server" Text='<%# Container.DataItem("Address") %>' />
</td>
<td align="center" class="content">
<asp:label id="lblGLA" runat="server" Text='<%# Container.DataItem("Type") %>' />
</td>
</tr>
</ALTERNATINGITEMTEMPLATE>
<FooterTemplate>
</table>
</div>
</td>
</tr>
</table>
</FooterTemplate>
</asp:repeater>