WordPress: Allow to only check one checkbox in a fieldset

Solution:

This script will make each checkbox per fieldset exclusive.

 

jQuery(function($) {
  $('fieldset input[type="checkbox"]').on('change', function() {
    // Set the checkbox just checked.
    let just_checked = $(this);
    // Get all of the checkboxes in the fieldset.
    let all_checkboxes = just_checked.closest('fieldset').find('input[type="checkbox"]');
    $.each(all_checkboxes, function(i, v) { // Loop through each of the checkboxes.
      if (just_checked.prop('id') === $(v).prop('id')) {
        // Check the one just checked.
        $(v).prop('checked', true);
      } else {
        // Uncheck the others.
        $(v).prop('checked', false);
      }
    })
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<fieldset id="field_3_1" class="gfield" data-field-class="gquiz-field">
  <legend class="gfield_label gfield_label_before_complex">Question 1</legend>
  <div class="ginput_container ginput_container_checkbox">
    <div class="gfield_checkbox" id="input_3_1">
      <div class="gchoice gchoice_3_1_1">
        <input class="gfield-choice-input" name="input_1.1" type="checkbox" value="gquiz21dc402fa" id="choice_3_1_1">
        <label for="choice_3_1_1" id="label_3_1_1">Answer 1</label>
      </div>
      <div class="gchoice gchoice_3_1_2">
        <input class="gfield-choice-input" name="input_1.2" type="checkbox" value="gquiz3414cb0c0" id="choice_3_1_2">
        <label for="choice_3_1_2" id="label_3_1_2">Answer 2</label>
      </div>
      <div class="gchoice gchoice_3_1_3">
        <input class="gfield-choice-input" name="input_1.3" type="checkbox" value="gquiz21d0214b9" id="choice_3_1_3">
        <label for="choice_3_1_3" id="label_3_1_3">Answer 3</label>
      </div>
    </div>
  </div>
</fieldset> An alternative solution could be to use radio buttons, and use css to make them look like checkboxes. But UX says that checkboxes shouldn't be used as radio buttons, and vice versa. For whatever that's worth.