Checkbox option to change background color of table cell

$('#selectall1').click(function(event) {
  if (this.checked) {
    $('.first').each(function() {
      this.checked = true;
    });
    $('.second').each(function() {
      this.checked = false;
    });
    $('.third').each(function() {
      this.checked = false;
    });
  } else {
    $('.first').each(function() {
      this.checked = false;
    });
  }
});

$('#selectall2').click(function(event) {
  if (this.checked) {
    $('.second').each(function() {
      this.checked = true;
    });
    $('.first').each(function() {
      this.checked = false;
    });
    $('.third').each(function() {
      this.checked = false;
    });
  } else {
    $('.second').each(function() {
      this.checked = false;
    });
  }
});

$('#selectall3').click(function(event) {
  if (this.checked) {
    $('.third').each(function() {
      this.checked = true;
    });
    $('.first').each(function() {
      this.checked = false;
    });
    $('.second').each(function() {
      this.checked = false;
    });
  } else {
    $('.third').each(function() {
      this.checked = false;
    });
  }
});

$(':checkbox').on('change', function() {
  var th = $(this),
    name = th.prop('name');
  if (th.is(':checked')) {
    $(':checkbox[name="' + name + '"]').not($(this)).prop('checked', false);
  }
});

$("input:checkbox[class='first']").click(function() {
  $(this).parent().toggleClass("highlight1");
});

$("input:checkbox[class='second']").click(function() {
  $(this).parent().toggleClass("highlight2");
});

$("input:checkbox[class='third']").click(function() {
  $(this).parent().toggleClass("highlight3");
});
div.highlight1 {
    background-color: #9FF781;
}
div.highlight2 {
    background-color: #F78181;
}
div.highlight3 {
    background-color: #8181F7;
}
div.highlight {
    background-color: #EEEEEE;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table border="1">
  <tr>
    <th>
      <INPUT type="checkbox" id="selectall1" />
    </th>
    <th>
      <INPUT type="checkbox" id="selectall2" />
    </th>
    <th>
      <INPUT type="checkbox" id="selectall3" />
    </th>
  </tr>
  <tr>
    <td>
      <div>
        <input type="checkbox" class="first" name="1" />
      </div>
    </td>
    <td>
      <div>
        <input type="checkbox" class="second" name="1" />
      </div>
    </td>
    <td>
      <div>
        <input type="checkbox" class="third" name="1" />
      </div>
    </td>
  </tr>
  <tr>
    <td>
      <div>
        <input type="checkbox" class="first" name="2" />
      </div>
    </td>
    <td>
      <div>
        <input type="checkbox" class="second" name="2" />
      </div>
    </td>
    <td>
      <div>
        <input type="checkbox" class="third" name="2" />
      </div>
    </td>
  </tr>
  <tr>
    <td>
      <div>
        <input type="checkbox" class="first" name="3" />
      </div>
    </td>
    <td>
      <div>
        <input type="checkbox" class="second" name="3" />
      </div>
    </td>
    <td>
      <div>
        <input type="checkbox" class="third" name="3" />
      </div>
    </td>
  </tr>
</table>

I want to change td background color when corresponding check box is checked. User must select only one check box among three. User must be able to select all check boxes at once when he wants and corresponding check boxes td background colors should change. I've some stuff but not completely done. I'm struct at some place and some redundancy is there.

Demo fiddle here

Answer №1

Here is a solution:

var selectall = $('.selectall');
selectall.click(function (event) {
    $('.' + $(this).data('class')).each(function () {
        this.checked = true;
        highlight(this, $(this).closest('td'));
    });
});

$('input[type=radio]').not(selectall).on('click', function() {
    highlight(this, $(this).closest('td'));
});

function highlight(radio, radioCell) {
    if (radio.checked) {
        radioCell.closest('tr').children('td.highlight').removeClass('highlight');
        radioCell.addClass('highlight');
    }
}
td:nth-child(3n + 1).highlight {
    background-color: #9FF781;
}
td:nth-child(3n + 2).highlight {
    background-color: #F78181;
}
td:nth-child(3n + 3).highlight {
    background-color: #8181F7;
}
div.highlight {
    background-color: #EEEEEE;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
    <tr>
        <th>
            <INPUT type="radio" name="selectall" id="selectall1" class="selectall" data-class="first" />
        </th>
        <th>
            <INPUT name="selectall" type="radio" id="selectall2" class="selectall" data-class="second" />
        </th>
        <th>
            <INPUT name="selectall" type="radio" id="selectall3" class="selectall" data-class="third" />
        </th>
    </tr>
    <tr>
        <td>
            <div>
                <input type="radio" class="first" name="1" />
            </div>
        </td>
        <td>
            <div>
                <input type="radio" class="second" name="1" />
            </div>
        </td>
        <td>
            <div>
                <input type="radio" class="third" name="1" />
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div>
                <input type="radio" class="first" name="2" />
            </div>
        </td>
        <td>
            <div>
                <input type="radio" class="second" name="2" />
            </div>
        </td>
        <td>
            <div>
                <input type="radio" class="third" name="2" />
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div>
                <input type="radio" class="first" name="3" />
            </div>
        </td>
        <td>
            <div>
                <input type="radio" class="second" name="3" />
            </div>
        </td>
        <td>
            <div>
                <input type="radio" class="third" name="3" />
            </div>
        </td>
    </tr>
</table>

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

The Google Contacts API is unable to utilize the max-results parameter when making requests via jQuery AJAX

Here is the code snippet I am using: $.ajax({ url: 'https://www.google.com/m8/feeds/contacts/default/full', dataType: 'jsonp', data: { access_token: token, max-results: '5000', alt: 'json' }, success:function(data){ ...

Running JavaScript code when the route changes in Angular 6

Currently, I am in the process of upgrading a website that was originally developed using vanilla JS and JQuery to a new UI built with Angular and typescript. Our site also utilizes piwik for monitoring client activity, and the piwik module was created i ...

What is the best way to get my loading screen div to cover the entirety of my screen, including all content beneath it?

In order to create a loading screen, I am utilizing PHP to dynamically load specific services for my website. I attempted to set the height of my wrapper div to 100% but it did not produce the desired results. After that, I experimented with setting the he ...

Ways to ensure the text on my website scrolls into view as the user navig

Is there a way to have my text show up as I scroll? I came across this , but I'm interested in how it actually functions. I saw a similar inquiry before, but it doesn't make sense to me. Can someone please explain or provide some examples on how ...

Ways to toggle the sliding of text on and off an image?

After spending some time experimenting with my code, I managed to get it working. Essentially, when a user hovers over an image, text now appears over the image. Since this is a gallery, I had to utilize $(this).children in order to target and display the ...

Only apply the CSS rule to all pages except for one

I'm a beginner when it comes to css. In my wordpress website editor (Onetone theme), I added the following rule to my 'custom css field': html, body {margin: 0; height: 100%; overflow: hidden} It's working well...the scrollbar is gone ...

Implementing a Countdown Clock in Auction Listings

Similar Question: Countdown to a specific date Is there a way to implement a jQuery countdown timer that starts from the day of posting an advertisement and ends on the expiry date? ...

Adding text on top of an image

I am having trouble with the master slider plugin as slide1, 2, and 3 each have unique classes. I attempted to overlay text on the image but it is not showing up. I tried using the following CSS code but nothing appeared on the screen. .classnameslide1: ...

Is there a way to produce a unique color using Stylus CSS?

Currently, I'm utilizing Express for Node.js and Stylus as the CSS engine. While Stylus is great, I'm facing some challenges in passing color variables or generating random colors. Despite attempting to use the javascript API for stylus, I find m ...

What is the best way to set a fixed width for my HTML elements?

I am facing an issue with my user registration form where error messages are causing all elements to become wider when they fail validation. I need help in preventing this widening effect. How can I achieve that? The expansion seems to be triggered by the ...

Trouble with Buttons not appearing on Datatables.net

Despite numerous attempts and thorough testing, I am unable to get buttons to appear on a datatables.net table. I have diligently added all the necessary scripts for buttons and followed every initialization example provided on datatables.net with no succe ...

Display information from a .js file onto an HTML webpage

I'm completely new to the world of server-side development and I'm attempting to navigate it on my own. Currently, I have a server written in JavaScript using Express and I'm trying to display some content on my HTML page that was sent from ...

Using JQuery to reveal a hidden child element within a parent element

I'm facing a challenge with displaying nested ul lists on my website. The parent ul is hidden with CSS, causing the child ul to also remain hidden even when I try to display it using jQuery. One approach I've attempted is adding a class to the f ...

Steps to trigger an alert when the entered quantity exceeds the current stock levels

After developing an IMS System, I encountered a problem where my stock is going into negative figures. To resolve this issue, my primary goal is to trigger an alert whenever the quantity entered by a user exceeds the available stock. For example, if a us ...

Is there a way to manipulate text in JQuery without altering the inner element?

I am struggling with an issue in my HTML code. Currently, I have the following structure: <h3 id="price">0.00<sup id="category">N/A</sup></h3> My intention is to use AJAX to replace the content within the <h3 ...

deleting the current product information during an ajax request

After executing the query provided below, I have $product_list in this code. However, I want to use ajax so that when I click on the button link1, $product_list gets emptied. How can I clear the content within products_list using an ajax call triggered by ...

Displaying various results using a range slider

I really need some assistance in making this range slider produce multiple outputs. I've attempted a few different options, but unfortunately, I haven't been able to figure it out. My goal is to have the text "590" display as 5.9 times the value ...

What is preventing relative paths from functioning with xsl:include?

I am working with an XSL file that converts to PDF. The top of the page contains a lengthy CSS style declaration: <xsl:attribute-set name="Header"> <xsl:attribute name="font-size"> <xsl:value-of select="$font-size"/> < ...

I encountered an error of "Unexpected token '>'" while working with an

My code includes an ajax call and utilizes promises in the function: element.on("keypress", ".keyEvents", function(event) { if (event.which == 13) { // create the url and json object var putUrl = ...

Using jQuery to reference my custom attribute---"How to Use jQuery to reference My

Can you explain how to reference a tag using a custom attribute in jQuery? For example, if I have a tag like this: <a user="kasun" href="#" id="id1">Show More...</a> I want to reference the tag without using the id. So instead of using: $( ...