Select all feature in checkbox is malfunctioning

I need to implement a feature with 3 checkboxes:

  • When the "A" checkbox is clicked, only "A" should be highlighted.
  • Upon clicking the "C" checkbox, only "C" gets highlighted.
  • If the "B" checkbox is clicked, both "A" and "B" need to be highlighted.
.a {
            background-color: #ffffff;
            font-family: Lato;
            font-size: 11.2px;
            font-weight: 500;
            text-align: center;
            color: #6a7c94;
            padding: 2px;
            width: 100px;
            height: 34.2px;
            margin-bottom: 15px;
            box-shadow: 0 2px 7px 0 rgba(0, 0, 0, 0.15);
        }

        #checkboxes input[type=checkbox] {
            display: none;
        }

        #checkboxes input[type=checkbox]:checked+.a {
            border-top: 2px solid #39cd90;
            color: rgb(57, 205, 144);
            padding-top: 0px;
        }
<div id="checkboxes">
          <input type="checkbox" name="rGroup" id="r1" />
          <label class="a" for="r1">A </label>
          <input type="checkbox" name="rGroup" id="r2" />
          <label class="a" for="r2">B </label>
          <input type="checkbox" name="rGroup" id="r3" />
          <label class="a" for="r3">C</label>
        </div>

When selecting "B", I want both "A" and "B" to be highlighted simultaneously for multiple selection. Here is the complete code. Any suggestions on how to achieve this functionality would be greatly appreciated.

Answer №1

Give this a shot!

$(document).ready(function() {

var checkbox1 = $("#r1");
var checkbox2 = $("#r2");

checkbox2.on('change', function(){
checkbox1.prop('checked',this.checked);
});

  /*Retrieve values */
  $('.a').click(function() {
    if(!$(this).prev().is(':checked')) {
  var value=$(this).prev().val();
  console.log(value);
  }
  });
});
.a{
   background-color: #ffffff;
  font-family: Lato;
  font-size: 11.2px;
  font-weight: 500;
  text-align: center;
  color: #6a7c94;
  padding: 2px;
  width: 100px;
  height: 34.2px;
  margin-bottom: 15px;
  box-shadow: 0 2px 7px 0 rgba(0, 0, 0, 0.15);
}

#checkboxes input[type=checkbox]{
    display: none;
}

#checkboxes input[type=checkbox]:checked + .a{
    border-top: 2px solid #39cd90;
  color: rgb(57, 205, 144);
  padding-top: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="checkboxes">
   <input type="checkbox" name="rGroup" value="1" id="r1" />
  <label class="a" for="r1">A </label>
  <input type="checkbox" name="rGroup" value="2" id="r2" />
  <label class="a" for="r2">B </label>
  <input type="checkbox" name="rGroup" value="3" id="r3" />
  <label class="a" for="r3">C</label>
</div>

Answer №2

I made some changes to your code. Take a look at the JavaScript section.

https://jsfiddle.net/s0wxewuv/13/

$(function() {
    $('#r2').on('change', function() {
        if ($(this).is(':checked')) {
            $('#r1').prop('checked', true);
            $('#r3').prop('checked', false);
        } else {
            $('#r1').prop('checked', false);
            $('#r3').prop('checked', false);
        }
    })
})

Answer №3

Give this a shot

$('#r2').on("click",function(){
  var r1 = $('#r1');
  r1.prop('checked', true);
});

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

Displaying a MySQL blob image in an HTML file with Vue.js: A step-by-step guide

Here is a Vue file that I have: export default { data(){ return{ info: { name: '', image: '', }, errors: [] } }, created: function(){ thi ...

Issue with Javascript function not triggering upon page navigation specifically in jQuery Mobile framework

In my jQuery Mobile application, I have two pages that are very simple. The first page contains only a single button: <a href="location.html" data-theme="b" data-role="button" data-icon="arrow-r">Create Report</a> When the button is clicked, ...

What is the best way to customize the appearance of a toggle switch using useStyles in Material UI, rather than withStyles?

I need help with customizing the styling of an input form switch in Material UI. I want the track and button to turn red when the switch is on, similar to this example. I have tried using the withStyles method following examples on the Material UI website, ...

Hover effect triggered upon mouse exit

As I delve into learning the basics of HTML and CSS, I came across the :hover property in CSS. This unique feature allows for a specific action to occur when the cursor hovers over an element, based on the code provided. Additionally, I discovered the tran ...

Extracting a compilation of video content from a YouTube channel through JSON data retrieval

I'm struggling to display the 5 most recent videos (title, updated, thumbnail (hqDefault)) from a specific channel using JSON data. I've tried various guides but can't seem to parse it correctly. Any suggestions on how to achieve this? I&apo ...

How can I align the CSS Horizontal Menu directly under the parent menu instead of its current incorrect x-position?

I am working on creating a horizontal drop-down menu using CSS. However, I'm facing an issue where the submenu either appears all the way to the left of the website (when I use position: absolute) or directly to the left of the menu (when set to posit ...

What is the best .scss file to use for updating the site?

Currently in the process of learning SASS and Bootstrap 4, I have set up some .scss partial files and imported them into my site.scss file as required. Making changes to Bootstrap involved modifying my _my-theme.scss file. But now, I'm wondering about ...

Is there no sign of rails being utilized in production?

There are some classic image png files located in app/assets/images/... However, in production, after running rake assets:precompile The images do not appear! Upon inspecting the CSS source code, I noticed that .logo { background-image: url("/ass ...

Is there a way to improve or simplify this jQuery validation code?

I am implementing client side validation using jQuery to ensure all fields are filled in before proceeding. var passedValidation = new Boolean(true); // Validate Required Fields if ($('#fbsignup input.firstName').val().trim() == '') { ...

Using Javascript to pass the value of a selected checkbox

I am having an issue with passing a row value to a different function when a user clicks on a checkbox in the last column of a table. The code I have written doesn't seem to be firing as expected. Can anyone help me figure out what might be missing in ...

Embed information from a MySQL database into an HTML layout

I have a main webpage containing various links (e.g. fist_link, second_link, etc...) When a user clicks on any link, it should open blank_template.html. If the user clicks on fist_link, the template should display the first string from the database table. ...

What is the reason behind the malfunction of the float?

Having an issue with the "1" form not extending to 100% width next to the "Tickets". Here is a visual representation of the problem: View how it currently looks here. html: <section id="main_content"> <div class="left inner"> ...

Unfortunately, Safari and iOS do not support the video functionality

I am encountering an issue with my HTML5 sample video not loading in Safari 11 on OSX, but working perfectly fine in Chrome and Firefox. Additionally, the video is not functioning on iOS at all (neither in Safari nor in Chrome). Below is the HTML code: & ...

jQuery is employed to create loading messages

I've encountered an issue with the loading message during my ajax call data retrieval process. It seems like the message either appears halfway through rendering until the ajax finishes or doesn't appear at all, leaving users confused. The loadin ...

How can information be seamlessly transferred between two HTML pages using a script?

For my listview, I need to pass values like "name", "info", "hap", "lat," and "lon" to page2.html. What is the best way to achieve this? In PHP, I typically use POST or GET methods, but is there a more efficient approach? If using GET is recommended, how ...

Are there ways to implement Vue.js transitions without directly setting the height in the code?

I'm having an issue with a Vue app I created where I need to make an element expand and collapse when a button is clicked. I want the effect to be smooth and animated, but using the <transition> tag alone isn't working as expected. The prob ...

Influencing various classes when :active is triggered

I encountered a challenge while working on an HTML project. I included a checkbox that should highlight all text input fields when checked. However, I'm facing an issue where some of the input fields within tables are not being affected by my code. An ...

Ways to extract value from all chosen dropdown list elements

<table id="tb_Answers"> <tbody> <tr> <td> <select class="ddl_NextQuestion" name="_ctl0"> <option value="0">End</option> <option val ...

Using JQuery to Give the TinyMCE Editor Focus: A Step-by-Step Guide

My current challenge involves adding text to the TinyMCE editor using SELENIUM. I have successfully added text with the following code snippet: $('iframe').last().contents().find('body').text('401c484b-68e4-4bf2-a13d-2a564acddc04& ...

What is the optimal method for invoking a Javascript function via a hyperlink?

I'm looking to include links on my website that will activate a javascript function when clicked, and I do not want these links to be underlined. Additionally, I want the cursor to change to a standard pointer. What is the most effective way to achiev ...