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
Just add checked
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:
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: