skip to Main Content
<div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px selected"><input class="" value="" type="radio" id="CRM_QFID_is_recur_radio" name="is_recur_radio"><label for="CRM_QFID_is_recur_radio">One-Time</label></div>

<input class="payment_processor_easy-pay crm-form-radio" value="4" type="radio" id="CRM_QFID_4_payment_processor_id" name="payment_processor_id">



<div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px"><input class="" value="month" type="radio" id="CRM_QFID_month_is_recur_radio" name="is_recur_radio"><label for="CRM_QFID_month_is_recur_radio">Monthly</label></div>

<input class="payment_processor_nach crm-form-radio" value="6" type="radio" id="CRM_QFID_6_payment_processor_id" name="payment_processor_id" checked="checked">

I have this 2 sets of button and I would like to automatically switch the radios. Condition is I can select only 1 radio.
For example when I select "CRM_QFID_is_recur_radio" button, "CRM_QFID_4_payment_processor_id" should be selected.

When I switch to CRM_QFID_month_is_recur_radio "CRM_QFID_6_payment_processor_id" should be selected.

How to do it in jquery? I have tried the following but it doesnt switch:

  $('input[name=is_recur_radio]').change(function() {
  $("input[name=is_recur_radio]:checked").attr("id") == "CRM_QFID_is_recur_radio" ? $('input[name=payment_processor_id][value=6]').prop("disabled", false) : $('input[name=payment_processor_id][value=6]').prop("disabled", true);
  $("input[name=is_recur_radio]:checked").attr("id") == "CRM_QFID_month_is_recur_radio" ? $('input[name=payment_processor_id][value=4]').prop("disabled", true) : $('input[name=payment_processor_id][value=4]').prop("disabled", false);
  $('input[name=payment_processor_id][value=6]').prop('checked', false);
  $('input[name=payment_processor_id][value=4]').prop('checked', false);
});

2

Answers


  1. If you have two radios:

    <input id="r1" type="radio">Radio 1<br>
    <input id="r2" type="radio">Radio 2<br>
    

    You can use the following code to do whatever you want when one is checked:

    // If one is checked, uncheck the other.
    $('#r1').change(function() {
      if ($(this).is(':checked')) {
        $('#r2').prop('checked', false);
      }
    })
    
    $('#r2').change(function() {
      if ($(this).is(':checked')) {
        $('#r1').prop('checked', false);
      }
    })
    

    To attempt to apply it to your situation:

    //  when I select "CRM_QFID_is_recur_radio" button, "CRM_QFID_4_payment_processor_id" should be selected.
    
    $('#CRM_QFID_is_recur_radio').change(function() {
      if ($(this).is(':checked')) {
        $('#CRM_QFID_4_payment_processor_id').prop('checked', true);
        $('#CRM_QFID_6_payment_processor_id').prop('checked', false);
      }
    })
    
    $('#CRM_QFID_month_is_recur_radio').change(function() {
      if ($(this).is(':checked')) {
        $('#CRM_QFID_6_payment_processor_id').prop('checked', true);
        $('#CRM_QFID_4_payment_processor_id').prop('checked', false);
      }
    })
    
    Login or Signup to reply.
  2. If the accepted answer does what you need, then this is much DRYer

    I gave the radios in the divs a class and use that to test

    Let me know if the code below also works for you

    $(".toggle-radio").on('click', function() {
      const isMonth = $(this).is("#CRM_QFID_month_is_recur_radio");
      $('#CRM_QFID_4_payment_processor_id').prop('checked', !isMonth);
      $('#CRM_QFID_6_payment_processor_id').prop('checked', isMonth);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    
    <div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px selected">
      <label><input class="toggle-radio" value="" type="radio" id="CRM_QFID_is_recur_radio" name="is_recur_radio">One-Time</label>
    </div>
    
    <input class="payment_processor_easy-pay crm-form-radio" value="4" type="radio" id="CRM_QFID_4_payment_processor_id" name="payment_processor_id">
    
    
    
    <div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px">
      <label><input class="toggle-radio" value="month" type="radio" id="CRM_QFID_month_is_recur_radio" name="is_recur_radio">Monthly</label>
    </div>
    
    <input class="payment_processor_nach crm-form-radio" value="6" type="radio" id="CRM_QFID_6_payment_processor_id" name="payment_processor_id" checked="checked">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search