Avatar of Elizabeth Shaules
Elizabeth Shaules
 asked on

Execute jQuery after Function

Hello,

I am embedding a form from HubSpot with this code:
<script>
  hbspt.forms.create({ 
    css: '',
    portalId: 'XXXXXXX',
    formId: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  });
</script>

Open in new window


On page load, this injects a DIV with a form in it.  This DIV has an identifying class "hbspt-form" (which I cannot control the name of, or adding ID's, etc.)    To style it, I am trying to execute this line of code after the above code:
jQuery('[name="firstname"]').addClass( "wpcf7-form-control wpcf7-text" );

Open in new window


Which doesn't work because its executing BEFORE the hbspt.forms.create function (as far as I can tell.)  If I run the jQuery line in the console, after all has loaded, the line works perfectly.  Can you help me create the code necessary to append those two classes at the correct time?

Thanks!
* hubspotCSSjQuery

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Elizabeth Shaules

ASKER
That API documentation was able to point me to "onReady" which let me figure out the rest of the solution:
hbspt.forms.create({ 
    css: '',
    portalId: 'xxxxxxxx',
    formId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    onFormReady: function($form) {

         document.getElementById('firstname-xxxxxxxxxxxxxxxxxxxxxxxxxxx').className += ' wpcf7-form-control wpcf7-text';

        } 
  });

Open in new window


The annoying part is that you can't wrap it in jQuery - it breaks the HubSpot code so it injects after the closing HTML tag, lol.
Elizabeth Shaules

ASKER
Thanks!
Julian Hansen

You are welcome.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck