• Resolved starepod

    (@starepod)


    We’re using a shortcode to embed a form that allows users to download a PDF after submitting the form. It was working fine until something changed with the plugin. We’re currently running v8.15.76. The error I’m getting is [forms-embed-1.2071] There was an error when running onFormSubmitted function from hbspt.forms.create TypeError: Cannot read properties of undefined (reading '0') v2.js?ver=8.15.76

    The error seems to be referring to the $form object passed to the ‘onFormSubmitted’ callback.

    Here’s the code that embeds the form. It’s partially generated by the plugin, with the ‘onFormSubmitted’ part added using the ‘leadin_forms_payload’ filter

    <script>
    
      window.hsFormsOnReady = window.hsFormsOnReady || [];
    
      window.hsFormsOnReady.push(() => {
        hbspt.forms.create({
          portalId: XXXXXXX,
          formId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          target: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
          region: "",
          onFormSubmitted: function( $form ){ // This is the snippet I added
    
            var class_list = $form[0].offsetParent.className;
    
            // Check for the wrapper class name
            if( class_list.includes( 'fl-insight-secondary' ) ) {
              // Open the PDF in a new tab/window
              simulateClick( 'https://xxx/xxx/xxx.pdf' );
            }
    
            return true;
    
          } // / onFormSubmitted function
    
        }) // / .forms.create
    
      });
    
    </script>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support harthur90

    (@harthur90)

    Hi @starepod , Thanks for your message!

    We recently changed to a new form script, so you will need to update your code to the following

    <script> window.addEventListener('message', event => {
    if (event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmitted') {
       // This is an example, please update accordingly 
       var form = event.target.document.querySelector("#hs-form-iframe-0").contentDocument.querySelector('#hsForm_${event.data.id}');
       if(form.classList.contains('fl-insight-secondary')){
    
       }
    }}); </script>

    Let me know if this helps

    Best regards,

    Thread Starter starepod

    (@starepod)

    Thanks for the tip, it definitely sent me in the right direction!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘$form object is undefined’ is closed to new replies.