skip to Main Content

How can I make the checkbox checkbox enabled by default? here is the code

<label class="checkbox woocommerce-form__label woocommerce-form__label-for-checkbox checkbox" data-automation-id="woo-commerce-subscription-opt-in">
                        <input type="checkbox" class="input-checkbox woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="mailpoet_woocommerce_checkout_optin" id="mailpoet_woocommerce_checkout_optin" value="1"> I have read and agree to the site terms and conditions *<span class="optional">(necessarily)</span></label>

Solution found thanks to @Eduhud Here is the code:

<script type="text/javascript">
jQuery( function($) {
    $( document ).ready(function() {
        setTimeout(function() { 
            $('#mailpoet_woocommerce_checkout_optin').prop('checked', true);
        }, 500);
        var checkout_is_updated = false;
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
            $( document.body ).on( 'updated_checkout', function(){
                $('#mailpoet_woocommerce_checkout_optin').prop('checked', true);
            });
        });
    });
});
</script>

3

Answers


  1. <script>
    (function($) {
        jQuery('#mailpoet_woocommerce_checkout_optin').prop('checked', true);
    })(jQuery);
    </script>
    
    Login or Signup to reply.
  2. Just add checked

    <input type="checkbox" class="input-checkbox woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="mailpoet_woocommerce_checkout_optin" id="mailpoet_woocommerce_checkout_optin" value="1" checked>
    
    Login or Signup to reply.
  3. If you have access to the html structure, the answer of @Ali jezzni should work, just add checked to the <input> field.

    In your case:

    <input type="checkbox" class="input-checkbox woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="mailpoet_woocommerce_checkout_optin" id="mailpoet_woocommerce_checkout_optin" value="1" checked>
    

    If you do not have access to the html structure, a good option is using jQuery to change the element once the page is loaded, as @Rakesh Sharma explains by targeting the element and using the .prop('checked', true) function of jQuery.

    In your case:

    <script type="text/javascript">
    jQuery( function($) {
        $( document ).ready(function() {
            setTimeout(function() { 
                $('#mailpoet_woocommerce_checkout_optin').prop('checked', true);
            }, 500);
            var checkout_is_updated = false;
            $('form.checkout').on('change', 'input[name="payment_method"]', function(){
                $(document.body).trigger('update_checkout');
                $( document.body ).on( 'updated_checkout', function(){
                    $('#mailpoet_woocommerce_checkout_optin').prop('checked', true);
                });
            });
        });
    });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search