• Resolved jussipv

    (@jussipv)


    Hi,

    I have forms on my site which are generated using jQuery after visitor makes certain actions on my site so the form is not on the site on initial page load.

    I am getting my forms HTML printed correctly with correct form ids etc. now but the problem is there’s no eventListeners for my forms because the forms are created after CF7 javascript is loaded.

    So how can I init my forms manually after CF7 javascript is already loaded and the form is generated after that on the page?

    I found this tutorial which mentions a function wpcf7.initForm($form); but apparently that is from an old version of the plugin:
    https://bensinger.me/contact-form-7-fixing-wpcf7-initform-not-function-error/

Viewing 1 replies (of 1 total)
  • Thread Starter jussipv

    (@jussipv)

    I actually managed to get this to work! This is my code that triggers when a new contact form is generated:

    jQuery(document).on("new_cf7_form_was_generated", function(e) {
     const forms = document.querySelectorAll('.wpcf7 > form');
     forms.forEach((e) => wpcf7.init(e));
    });

    I know it’s missing the checks for wpcf7 being defined and browser support for fetch and formdata but I removed them to make the code example more clear.

Viewing 1 replies (of 1 total)
  • The topic ‘How to init forms manually?’ is closed to new replies.