0

I have 3 checkbox

  • On click of "A" checkbox the A has to be highlighted.
  • On click of "C" checkbox the "C" gets highlighted.
  • On click of "B" checkbox the "A" and "B" has 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>

On click of "B" how to get highlighted both "A" and "B" as multiple selection. Here is my complete code How to achieve this give some suggestions.

Sankar
  • 6,908
  • 2
  • 30
  • 53
Sam
  • 1,381
  • 4
  • 30
  • 70
  • so u want to get highlighted one at a time or whatever you are clicking need to be highlighted along with the previous one you question is not clear. – Vishal Taj PM Mar 27 '18 at 04:08
  • @ Vishal Taj PM on click of "B" checkbox both "A" and "B" should get highlighted and again if i click on"B" checkbox both "A" and "B" should uncheck – Sam Mar 27 '18 at 04:11

3 Answers3

0

Try this

$('#r2').on("click",function(){
  var r1 = $('#r1');
  r1.prop('checked', true);
});
  • @lakshmipriya you need to place this code in document.ready: Like this: `$()document).ready(function(){ $('#r2').on("click",function(){ var r1 = $('#r1'); r1.prop('checked', true); }); })` – jarvo69 Mar 27 '18 at 04:20
0

I modified you code. Check out the js part.

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);
        }
    })
})
shrekuu
  • 1,716
  • 1
  • 23
  • 38
0

Try this

$(document).ready(function() {
 
 var chk1 = $("#r1");
 var chk2 = $("#r2");
 
 chk2.on('change', function(){
  chk1.prop('checked',this.checked);
 });

  /*For getting values */
  $('.a').click(function() {
    if(!$(this).prev().is(':checked')) {
   var val=$(this).prev().val();
   console.log(val);
   }
  });
});
.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>
Athul Nath
  • 2,536
  • 1
  • 15
  • 27