Utilize JavaScript to reveal a label upon checking a checkbox within an ASP.NET repeater control

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>

Answer №1

Consider utilizing onClientClick= "verifyReport()" in place of the onClick function on the checkbox element. The onClientClick attribute executes javascript code on the client side.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS selector targeting the value inside a textbox

I am working in an environment that only supports CSS and Bootstrap 3.2, with no JavaScript functionality. My current task involves creating a list filtering solution using pure CSS selectors and HTML. Imagine I have 2 items within an HTML list element. T ...

I encountered an error with the TS1003 code in Angular because an identifier was expected on the throw import statement. Upon further investigation, I realized that the issue originated from

I came across this piece of code: import { Observable, throw} from 'rxjs'; An error message popped up saying identifier expected: ERROR in config/config.service.ts(3,22): error TS1003: Identifier expected. The error appears to be related to ...

How can I resolve the issue of <td> being repeatedly displayed five times instead of just twice in PHP?

Can someone assist me with fixing this for loop issue? I am trying to display controls next to each item in the row, but it is showing 5 sets of controls instead of just 2. <tbody> <?php //retrieve list of supplies $numOfRows = 0; $result = my ...

An error has occurred in Node.js: SyntaxError - Unexpected token ]

Here is the code snippet I am working with: app.get("/posts/:slug", function(request, response) { var slug = request.params.slug; connection.query("SELECT * from `posts` WHERE slug = ?", [ slug ], function(err, rows) { var post = rows[]; ...

Receiving a "Bad Request" error when trying to access a website

Every time I attempt to call a lengthy URL, I encounter a Bad Request issue. https://localhost:44320/RespostaEmail/96635/750396/[%7B%22IdItem%22:8,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:1,%22IdTipoReposta%22:80%7D,%7B%22IdItem%22:3,%22IdTipoReposta%22:8 ...

The functionality of "Body Onload" for sending "ScrollHeight" is malfunctioning in Chrome and Safari

I came across an issue where the iframe_resize function in my code was not working as expected. After investigating further, I realized that the problem did not lie with the function itself. So, I decided to post a new question. Within my index.html file ...

Refreshing the information in the database table

Upon receiving data from the server using ajax, I populate this table: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +data[i].B ...

Utilizing the Singleton Pattern to Prevent Null Values in Asp.Net Sessions

I need the "GetClassTeacher" method in my Asp.net application to be executed only once per session. To achieve this, I am currently using a session check to ensure that the object is null before calling the database. My query is whether this approach is t ...

Should I reload the entire table or insert a row manually?

This unique ajax question arises: within a table lies the users' information, displayed based on individual settings and timing. Sometimes, users instantly see the data, other times they must wait for it - their choice determines when it appears. Wha ...

One way to determine whether .ajax is using Get or POST is to check the type parameter

I have a query: $.ajax({ url: "http://twitter.com/status/user_timeline/treason.json?count=10&callback=?", success: function (data, textStatus, jqXHR) { }, error: function (jqXHR, textStatus, errorThrown ...

Is there a way to access the body html element within a template in vue.js?

I'm struggling to add styles to the body element of my HTML page. It seems like I can't access it directly from the template because there's another body element nested inside the main body. Even when I try to change the style using JavaScri ...

AngularJS not compatible with Splice functionality

Currently, I am working on a form for an Items list that allows users to add, edit, and delete items. While the add and edit functionalities are working correctly, I am facing some issues with the delete functionality. Below is a snippet of my code that i ...

Retrieving a specific field from an object within an array

Struggling with Meteor and MongoDB, I am having trouble accessing a specific field from objects within an object array. Details of my documents: { "_id" : "p6c4cSTb3cHWaJqpG", "createdAt" : ISODate("2016-05-11T11:30:11.820Z"), "username" ...

Featherlight is experiencing issues with running Ajax requests

I'm currently working on integrating an ajax photo uploading script into a Featherlight lightbox, but I'm running into issues! If anyone could help me figure out what's going wrong, that would be greatly appreciated. I've already includ ...

Struggling to access a web API directly from a hyperlink provided in an email

During my testing of a webapi that sends email notifications with a verification link for users, I encountered an issue. Although I received the email, clicking on the link did not successfully verify the user as expected. Instead, I encountered the follow ...

Unable to load skybox using CubeTextureLoader in three.js

I am struggling to figure out what I am doing wrong in this situation. My goal is to load a simple skybox, but I am not seeing any results. Most solutions I have found reference the THREE.ImageUtils.loadTextureCube method which is now deprecated. I can&apo ...

"Encountered a reference error in Node.js Express due to an undefined

const _expressPackage = require("express"); const _bodyParserPackage = require("body-parser"); const _sqlPackage = require("mssql"); //Initializing the app with the express web framework ...

What is the alternative method of invoking a function within another function in React Native without utilizing a constructor?

Within my RegisterTaster function, I need to execute another function called endRegisterAlert. Since I'm not using a constructor and instead treating the class as a const in React Native, how can I achieve this? What is the best way to call the endRe ...

JavaScript updates the cursor after the completion of the JS function

Here is some code that I have been working with: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> </head> <body style="background-color:yellow;width ...

What is the method to retrieve the base host in AngularJS?

I need assistance with the following URL: https://192.168.0.10/users/#!/user-profile/20 When I use $location.host, it returns 192.168.0.10 However, I only want to extract https://192.168.0.10 What is the best way to achieve this? ...