Detect and warn on radio change

by helgatheviking

HTML

<fieldset id="fields" data-original="a">
<legend>
some radios
</legend>
<label><input type="radio" name="test" value="a" checked="checked">A</label>
<label><input type="radio" name="test" value="b">B</label>

</fieldset>

<div id="result">
</div>

JavaScript

$('body').on('change', ':radio', function(e) {

  if (this.value === 'b') {
    let result = confirm('Selecting B will cause some schenanigans are you sure you want to proceed?');

    if (result) {
      $('#result').html('ok we did some schenanigans');

    } else {
      let value = $('#fields').data('original');

      $('#fields').find('input[value="' + value + '"]').prop('checked', true);


      e.stopPropagation();
    }
  }


});