Gridview featuring a pair of multi-select checkboxes within a column

When clicking on the select all header checkbox, only the checkboxes in that specific column should be selected. The same rule should apply for both columns.

Currently, the code is selecting and unselecting both columns at once when any checkbox is clicked. However, I want them to function independently from each other. This means that when the header checkbox is selected, only the checkboxes in that particular column should be affected, and vice versa for the other column.

<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
    <HeaderStyle Width="50px" HorizontalAlign="Center" />
    <ItemStyle HorizontalAlign="Left" />
    <ItemTemplate>
        <asp:CheckBox ID="chkStatus" runat="server" Width="15px" Checked='<%# Convert.ToBoolean(Eval("isAssignJD")) %>' />
    </ItemTemplate>
    <HeaderTemplate>
        <asp:CheckBox ID="headerChkbox" runat="server" onclick="javascript:SelectAllCheckboxesSpecific(this);" />
    </HeaderTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="CheckBoxTemplate">
    <HeaderStyle Width="50px" HorizontalAlign="Center" />
    <ItemStyle HorizontalAlign="Left" />
    <ItemTemplate>
        <asp:CheckBox ID="chkUpdate" runat="server" Width="15px" Checked="false" />
    </ItemTemplate>
    <HeaderTemplate>
        <asp:CheckBox ID="headerUpdate" runat="server"  onclick="javascript:SelectAllCheckboxesSpecific(this);" />
    </HeaderTemplate>
</telerik:GridTemplateColumn>

Below is the function:

function SelectAllCheckboxesSpecific(spanChk) {
    //  Select checkboxes within the grid
    var IsChecked = spanChk.checked;
    var Chk = spanChk;
    Parent = document.getElementById('ctl00_ContentPlaceHolder1_gvJobPosition');
    var items = Parent.getElementsByTagName('input');
    for (i = 0; i < items.length; i++) {
        if (items[i].id != Chk && items[i].type == "checkbox") {
            if (items[i].checked != IsChecked) {
                items[i].click();
            }
        }
    }
}

Here is the rendered HTML code:

<tr class="rgRow" id="ctl00_ContentPlaceHolder1_gvJobPosition_ctl00__0">
    <td style="display:none;">0561fb4f-e410-4d83-a0e5-6d6d68fe3dba</td><td align="left">
                                    <span class="category1" style="display:inline-block;width:15px;"><input id="ctl00_ContentPlaceHolder1_gvJobPosition_ctl00_ctl04_chkStatus" type="checkbox" name="ctl00$ContentPlaceHolder1$gvJobPosition$ctl00$ctl04$chkStatus" checked="checked" /></span>
                                </td><td align="left">
                                    <span class="category2" style="display:inline-block;width:15px;"><input id="ctl00_ContentPlaceHolder1_gvJobPosition_ctl00_ctl04_chkStatuss" type="checkbox" name="ctl00$ContentPlaceHolder1$gvJobPosition$ctl00$ctl04$chkStatuss" /></span>
                                </td>

Answer №1

Your issue lies here:

const checkboxes = document.getElementsByTagName('input');

You're targeting all checkboxes on the page. Have you considered using jQuery to simplify this process? Here's a more straightforward approach:

Add class "category1" to all checkboxes in category 1, and "category2" to those in category 2. Also, assign "setAll1" and "setAll2" classes to the header checkboxes. Then, use the following script:

$(document).ready(function(){
    $('.setAll1').click(function(){
        if($('.setAll1').is(':checked'))
            $('.category1').prop('checked', true);
        else 
            $('.category1').prop('checked', false);
    });
    $('.setAll2').click(function(){
        if($('.setAll2').is(':checked'))
            $('.category2').prop('checked', true);
        else
           $('.category2').prop('checked', false);
    });
});

For the HTML part:

<body>
    <input type=checkbox class="setAll1"/>
    <input type=checkbox class="category1"/><input type=checkbox class="category1"/><input type=checkbox class="category1"/>
    <br/><br/><br/>
    <input type=checkbox class="setAll2"/>
    <input type=checkbox class="category2"/><input type=checkbox class="category2"/><input type=checkbox class="category2"/>
</body>

There are multiple ways to achieve this with jQuery, but the above method is a simple solution that came to mind first.

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

How to Automatically Close an Ajax Modal Popup After Executing Code in ItemCommand in ASP.Net

I am currently facing an issue with closing a ModalPopup after a code sequence is executed. The situation at hand involves coding a filebrowser for the company and everything seems to be working fine except for the downloading of files. I have implemented ...

Dynamic content moves unpredictably when adding or modifying TextBoxes or DropDownList items

Whenever I navigate from one textBox to another in my .aspx form, it starts jumping around. The problem is exacerbated when I switch focus to or from a DropDownList. I only have a few textboxes on the form and nothing complex like gridviews or labels. So ...

Searching for the most recent entry from a related group within an HTML table using jQuery

I have a list of students in different classes that looks like this Class StudentName RollNo. Class X Sarah 1 Class Y David 2 Class Z Emily 3 Class W Adam 1 C ...

JavaScript is unable to make changes to the page once it has finished loading

I have implemented a JavaScript code that allows me to send messages in a chat interface: function sendMessageToChat(conversation, message) { $("#content").html(conversation + message); $(".current-msg").hide(); $(".current-msg").delay(500).fadeIn ...

Lifetime duration set for reset password token that is generated by a IUserTokenProvider as the default

Can anyone provide information on the default expiration time of a token generated by IUserTokenProvider? I am utilizing EmailTokenProvider to create a token for password reset purposes. However, I am unsure about its duration. I am aware that the expirat ...

If using conditional SCSS, consider overriding the variables

When working with React state, I am passing a list and calling the component where needed. comments: { elementLabel: "Comments", elementType: 'textarea', className: "form-control", ...

Issues encountered with the performance of a Laravel Vue.js application

I am encountering a recurring issue when trying to run new applications that combine Laravel with Vue.js. This problem persists whether I am working on a partially developed app or starting from scratch with a fresh Laravel installation. Upon setting up La ...

Is it possible to identify the origin URL of a user using Javascript or AngularJS?

Is it possible to use Angular to detect the origin URL of a user visiting my website in order to perform specific operations later on? ...

What steps are involved in creating a function within AngularJS?

Just starting out with AngularJS and looking to implement a shopping cart using WebSQL. Wondering how to create functions for adding items to the cart and removing them. Here's the code snippet I have so far: angular.module('ecommerce').fa ...

Can you provide an update on the progress of the JavaScript target development in antlr3?

What is the current status of Antlr3's Javascript target? I attempted to generate a parser for a simple grammar, but encountered numerous compiler errors in the generated code. After investigating the website, I downloaded the latest antlr3.5-snapshot ...

Is the AJAX form submitting back to its own page?

I'm experiencing some issues with AJAX on my website tonight. After submitting a form, the page refreshes with the values in the URL instead of performing the AJAX request. I have a validate plugin with a submit handler in place, but it doesn't s ...

Using jQuery to handle multiple buttons with the same ID

Is there a way to address the issue of multiple buttons sharing the same id? Currently, when clicking on any button, it only updates the record with id=1. How can this problem be resolved? div id="ShowPrace"> <?php try { $stmt = $pdo->prepare(" ...

Unable to render Row using MySQL in conjunction with ReactJS

Recently, I started working on developing a table using ReactJS on the FrontEnd, NodeJS on the BackEnd, and MySQL for the database. I am trying to retrieve data based on a Select request using the primary key (Code) from the list. https://i.sstatic.net/tXP ...

Using more than one WebGLRenderer on a shared canvas

I am attempting to have multiple WebGLRenderer instances render on the same canvas. It seems to work initially for the first frame, but when it comes to animating, things start to break down. I'm curious if there are any limitations with this approach ...

creating a random integer and seamlessly transmitting it to the header

When a user enters an incorrect email or password in the login form, I redirect them to login.php?Err=284613 using header ('Location: login.php?Err=284613');. Then, I use Javascript to detect this error code on the page with if (window.location.s ...

Using JavaScript, conceal a specific Div by examining the content within another Div

I am attempting to implement some logic using JavaScript. The goal is to hide the "cart-button" div if the innerHTML value of a div with the class "b-format" is set to Audio, otherwise hide the "more-button" div. However, for some reason this functionality ...

The REGEX Pattern ignores every second entry

Seeking assistance with two different REGEX patterns used to interpret MGRS grid coordinates inputted by users. The first pattern is designed for coordinates without spaces: /\d{1,2}[^ABIOYZabioyz][A-Za-z]{2}([0-9][0-9]){0,5}/g Here are some examples ...

Refresh the page to change the section using vue.js

I am currently working on a website using Laravel and Vue.js. I require two separate sections for the site: Site: https://www.example.com Admin: https://www.example.com/admin Within the resource/js/app.js file, I have included the main components as fo ...

add items to the dropdown using a loop

I am encountering an issue with my bootstrap dropdown. I am trying to loop a specific number of times and append each loop number inside a li. However, my current code is not working as expected. Can anyone provide guidance on how to fix this? Thank you in ...

Pattern for identifying JavaScript import declarations

My task involves using node to read the contents of a file. Specifically, I am looking to identify and extract a particular import statement within the file that includes the "foo-bar" package. The goal is to match only the line or lines that contain the ...