If checkboxes are found within the table's rows and cells, add the parent row class if none of the

I am attempting to implement a small script within my table under the following conditions: The table contains a checkbox within each tr td, and I want my jQuery script to be applied only if the selector identifies a tr where all td checkboxes are unchecked. If only one checkbox is checked within a tr row, then I do not want to add a class to the parent tr.

Essentially, I need to add a class to the parent tr only when none of the td checkboxes within the entire tr row are selected.

Below is my script:

$(function () {
    $('#callScr').on('click', function (e) {
        $(".table tr input:not(:checked)").each(function (i, obj) {
            $(this).parent().parent().addClass("yourClass");
        });
    })
});

Link to my fiddle:

Currently, the script is adding the class to all parent tr elements, even if one checkbox is selected...

Answer №1

If you want to also remove a class when a checkbox is checked, you can use the following jQuery code:

$(function(){
    $('#callScr').on('click', function(e){
        $('.table tbody tr').each(function(){
            $(this).toggleClass('yourClass',$(this).find(':checkbox:checked').length == 0);
        });
    });
});

Check out this example on JSFiddle!

Answer №2

Iterate over each tr within the tbody and calculate the number of checked checkboxes inside it. If the count is 0, then add a specific class.

$(function () {
    $('#executeFunction').on('click', function(e) {
        $(".table tbody tr").each(function(index, element) {
            if ($(this).find(":checkbox:checked").length == 0)
                $(this).addClass("selectedStyle");
        });
    });
});

UPDATED FIDDLE

Answer №3

To achieve this, you can loop through the tr elements and perform the necessary actions while checking the input elements within them.

$(function(){
    $('#callScr').on('click', function(e){
       $( ".table tr:gt(1)" ).each(function(i, obj) {
          if($(this).find('input:checked').length===0)
             $(this).addClass( "yourClass" );
       });
    });
});

Check out this JS Fiddle link for further details

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

Is there a way to connect an HTML page without using a hyperlink?

Is there a way to directly display linked HTML pages on my webpage instead of just creating a link? I'm looking for suggestions on how to arrange this. Thank you! ...

Tips for retrieving and displaying values from a form using only PHP

I have a simple request: I want to input a name, click the submit button, and then display the name below the form. Seems easy right? <?php echo $_POST['one']; ?> <!DocType HTML> <html> <body> <p> ...

Close popup after submitting the form

On my website, I have a function to submit a form within a modal. Everything works well except for when I uncomment a certain line of code. When I uncomment that line, my test mysql insert on the page /index.php/trainings/add doesn't get executed. I a ...

Sending jQuery events from an aspx page to an ascx control

Yesterday, I began utilizing ascx and started breaking down my "dynamic" default.aspx page into smaller segments. In default.aspx, there is a significant amount of JavaScript/jquery code responsible for managing various events. After moving some html code ...

The PHP file on the server is missing the mandatory "event" parameter for the EventSource

Explaining this issue was a bit of a challenge. I've set up a Javascript EventSource object with some customized event handlers like so: var source = new EventSource('updates.php'); source.addEventListener('add', addHandler, fals ...

Pause until the existence of document.body is confirmed

Recently, I developed a Chrome extension that runs before the page fully loads by setting the attribute "run_at": "document_start". One issue I encountered is that I need to insert a div tag into the body of the webpage as soon as it is created. However, a ...

Missing table header in HTML causing table to not display correctly

I currently have an HTML table that consists of three columns and a varying number of rows, depending on the output from a database. var fields = ['a','b','c']; //variable from database var data = ['p', & ...

Is there a way to add 100 headings to a webpage without using a loop when the page loads

Just joining this platform, so please be patient with me! The task at hand is to insert 100 h3 headings on page load ("Accusation 1, Accusation 2, Accusation 3,...Accusation 100"). We are restricted to using only 1 loop throughout the lab, which will also ...

Tips for displaying a Bootstrap 4 table on smaller screens

I have a Bootstrap 4 table filled with JSON data. The table renders perfectly on the UI, but users on smaller devices must scroll to view all the data. Despite there not being much data in the HTML table, users still need to scroll on small devices. I wa ...

Issue encountered in SQL PHP form script at line 23

I'm currently working on some code to insert data into different tables, but there seems to be an issue on line 23. This code is part of a college project registration form. <?php $con = mysql_connect(","",""); if (!$con) { die('Could not c ...

Surprising outcome from the glob-fs glob.readdirSync function

Below is some nodejs code that I am working with. The client initially calls /api/demosounds and then calls /api/testsounds. var glob = require('glob-fs')({ gitignore: true }); app.get('/api/demosounds',function(req,res){ var d ...

stop automatic resizing of windows

My CSS is written using percentages. I need to maintain the percentages intact. Is there a way to stop the layout from changing when the browser window is resized? I want the percentages to remain unaffected, even when the browser window is being resized ...

Refining to Showcase One Menu Selection

I am having an issue with my bootstrap menu that includes a search field. The problem is that when I try to filter the dropdown menu using the search field, it filters all dropdown items instead of just the one I want. For example, if I search for a term f ...

What is the best way to display a child component inside an iframe using Vue.js?

Looking to provide a live preview of an email before sending it out, I've opted to use an iframe to contain the preview and prevent style leaks. The goal is for the preview to update dynamically as the user fills out form details. How can I display a ...

Applying CSS link dynamically in NextJS based on the window width

Is it feasible to adjust NextJS link stylesheets depending on media queries in the context of using css modules? Here is an example: CSS module file: @media(max-width: 800px) { .myClass{ /* ...mobile styles */ } } @media(min-width: 800px) { .myC ...

What is the best approach for sending a binary image post request to an API using nodejs?

I am receiving the image binary in this manner: axios.get("image-url.jpg") Now, I want to utilize the response to create a new POST request to another server const form = new FormData(); const url = "post-url-here.com"; form.appe ...

Leverage node rework CSS directly within your browser for seamless website

Looking to utilize the reworkcss/css library in the browser. Downloaded version 2.0.0 from GitHub and installed necessary packages with npm install. Attempted using requireJS, which supports processing the CommonJS Module Format by requiring index.js, bu ...

Adding a timestamp to the src URL in HTML: A step-by-step guide

Looking for a way to implement cache busting in my index.html file. Take a look at the code snippet below: <body> <app-root></app-root> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="&l ...

What is the best way to compare two times in the hh:mm AM/PM format?

I need to handle times in the format of hh:mm AM/PM generated by a specific plugin, and perform comparisons on them using jQuery/javascript. When a user manually inputs a time, I require the text in the textbox to automatically adjust to hh:mm AM/PM with ...

Tips for utilizing multiple ngFor directives for property binding within a single directive

After implementing the ng-drag and drop npm module with the draggable directive, I encountered an issue while trying to display a list of items from a 2D array using li elements. Since multiple ngFor's are not allowed in Angular, I needed to come up w ...