Gform datepicker options pre init only work after submit form?

Hi,

I have added this code in footer.php template:

gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
if ( formId == 2 && fieldId == 4 ) {
    optionsObj.firstDay = 1;
    optionsObj.beforeShowDay = jQuery.datepicker.noWeekends;
}
return optionsObj;

});

My form loaded by shortcode on specific template page:

echo do_shortcode('[gravityform id="2" title="false" description="false" ajax="true"]');

Problem:
DatePicker change only work after form submit.

How to correct this ?
Best,

Have you tried adding the gform_datepicker_options_pre_init script to an HTML field in the form where you want it to apply? It will be applied before the form is submitted. The other way to add the script is be enqueuing a custom JavaScript file. Adding the script to the footer.php is not a supported method of adding the script for your datepicker.

Hi,
" script to an HTML field in the form where you want it to apply?" don’t understand how, tried to add html field and put gform code on it but it’s showed as text…

Best,

Hello @ilanb - you will need to add the opening <script> tag and closing </script> tag around the actual script when you add it to an HTML field in the form. Otherwise, it will show up as text, as you saw.

Thanks Chris But tag was removed when I save form…

Envoyé de mon iPhone

If you add an HTML field to the form, and in the dashboard form editor, add the script enclosed by <script> and </script> tags, it should work. Saving the form with script tags in the HTML field works for me. If that is not working for you, I recommend opening a support ticket: https://www.gravityforms.com/support/

Please take a look at this article for alternate ways to add the script to your site if you do not want to open a support ticket:

[quote=“ilanb, post:1, topic:6296”]

gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
if ( formId == 2 && fieldId == 4 ) {
    optionsObj.firstDay = 1;
    optionsObj.beforeShowDay = jQuery.datepicker.noWeekends;
}
return optionsObj;
});

Thanks Chris,

After some test, problem was from classic editor, I need to use this to keep tag…

    function km_add_unfiltered_html_capability_to_editors( $caps, $cap, $user_id ) {
    if ( 'unfiltered_html' === $cap && user_can( $user_id, 'administrator' ) ) {
        $caps = array( 'unfiltered_html' );
    }
    return $caps;
}
add_filter( 'map_meta_cap', 'km_add_unfiltered_html_capability_to_editors', 1, 3 );

But I have another error now:

ReferenceError: gform is not defined

Best,

Hello. What is the URL to the page on your site where we can load the form and check that console error?