How to reset all radio buttons except for the one with a checked attribute?

Viewed 323

I am attempting to reset a form field by wiping all checked properties and then applying the checked property back to the one with a "checked = 'checked'"attribute.

However, I'm not sure how to only select the element that passes my if statement, as console.log(thingo) is logging all three checkboxes.

$('a').click(function () {
  if ($(this).parents('.form-field').find('.form-check-input').length != 0) { // does the revert button belong to a group with radio button inputs?    
    $(this).parents('.form-field').find('.form-check-input').prop('checked', false); //clear all checkboxes
    var thingo = $(this).parents('.form-field').find('.form-check-input');  //set checkboxes to thingo

    if (thingo.attr('checked') != null) {
      console.log(thingo);
      thingo.prop('checked', true); //attempt to set all checkboxes with attribute checked to be checked
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"> </script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="form-group form-field">
  <label class="form-check-label">
    Sex
    <span>[<a href="javascript:void(0);">Revert</a>]</span>
  </label>
  <div class="form-check">
    <input type="radio" class="form-check-input" value="0" checked="checked">
    <label class="form-check-label">Male</label>
  </div>
  <div class="form-check">
    <input type="radio" class="form-check-input" value="1">
    <label class="form-check-label">Female</label>
  </div>
  <div class="form-check">
    <input type="radio" class="form-check-input" value="2">
    <label class="form-check-label">Unclear</label>
  </div>
</div>

3 Answers

This can be quite simply solved by using Jquery's attribute selector. All I have to do is:

 $(this).parents('.form-field').find('.form-check-input').prop("checked", false);  
//^this line clears all existing checks          
 $(this).parents('.form-field').find("[checked = 'checked']").prop("checked", true);
//^this line finds the checked attribute with the attribute selector and sets it to checked
$('button').on('click',function () {
    var formField = $(this).parents('.form-field');
    var formFieldInputs = formField.find('.form-check-input');
    if (formFieldInputs.length){
        formFieldInputs.prop("checked", false); //clear all checkboxes
        formField.find('.form-check-input-default').prop("checked", true);
    }
});

HTML format, just add a default class

<div class="form-group form-field">
       <label class="form-check-label">
           Sex
           <span>[<button>Revert</button>]</span>
       </label>
       <div class="form-check">
           <input type="radio" class="form-check-input form-check-input-default" value="0" checked />
           <label class="form-check-label">Male</label>
       </div>
       <div class="form-check">
           <input type="radio" class="form-check-input" value="1" />
           <label class="form-check-label">Female</label>
       </div>
       <div class="form-check">
           <input type="radio" class="form-check-input" value="2" />
           <label class="form-check-label">Unclear</label>
       </div>
</div>

Some elements here:

Related