0

I need to have 2 checkbox work as radiobutton, but I can't use radiobutton cause I want to make uncheck available

<script type="text/javascript">
$(document).ready(function() {
$(".gps-garmin").click(function() {
  selectedBox = this.id;
  $(".gps-garmin").each(function() {
    if ( this.id == selectedBox )
    {
      this.checked = true;
    }
    else
    {
      this.checked = false;
    };
  });
 });
});

<td><input type="checkbox" id="garmin" value="garmin" class="gps-garmin"></td>
<td><input type="checkbox" id="gps" value="gps" class="gps-garmin"></td>

Actually my checkbox are working exactly like radio button, I found this code here, but I need to make uncheck possible

Sydowh
  • 99
  • 2
  • 11
  • 1
    `I can't use radiobutton cause I want to make uncheck available` In this case use 3 radio buttons. 1 for each of these options: `Garmin`, `GPS` and `None` This makes the validation logic simpler too, as then all groups must be chosen from, so theres no possibility of a user missing out an option – Rory McCrossan Feb 13 '17 at 13:58
  • I don't want a none button it's useless in my case, I got other checkbox after this that didn't need this feature – Sydowh Feb 13 '17 at 13:59

3 Answers3

1

Try this:

$(".gps-garmin").click(function() {
  var check = $(this).prop('checked');
  $(".gps-garmin").prop('checked',false);
  $(this).prop('checked', (check) ? true : false);
  });
Anton Zikov
  • 144
  • 8
0

As mentioned in How-to-uncheck-a-radiobutton, you should be able to clear a radio-button by using plain js or jQuery

For example in case of using plain js (copied from the linked answer):

this.checked = false;
Community
  • 1
  • 1
0

Is this You trying: $(".gps-garmin").click(function() {

if($(this).prop("checked") == true){

$(".gps-garmin").prop('checked',false);

$(this).prop('checked',true);

} else if($(this).prop("checked") == false){

$(".gps-garmin").prop('checked',false);


        }

});

Rajesh Baskaran
  • 322
  • 1
  • 11