Home > OS >  Select radio button with jQuery in woo commerce
Select radio button with jQuery in woo commerce

Time:03-06

I want to select/check a radio button when another radio is selected. I'm using .prop('checked', true); but it is not working.

This is what I have:

 add_action( 'woocommerce_after_add_to_cart_form', 'conditionally_hide_show_new_field', 9999 );
      
    function conditionally_hide_show_new_field() {
        
      wc_enqueue_js( "
         
    $('input[type=radio][name=testradio]').change(function() {
        if (this.value == 'my_value') {
           $('#testradio').prop('checked', true);
        }
    });
      ");
    }

CodePudding user response:

Does your change event fire on radio button change? As your code is depending on jQuery and DOM loaded try wrapping your code with $(function() {} so it will execute once jQuery has loaded:

add_action( 'woocommerce_after_add_to_cart_form', 'conditionally_hide_show_new_field', 9999 );
      
function conditionally_hide_show_new_field() {        
    wc_enqueue_js( "
      $(function() {     
        $('input[type=radio][name=testradio]').change(function() {
          if ($(this).val() == 'my_value') {
            $('#testradio').prop('checked', true);
          }
        });
      });
    ");           
}
  • Related