I need to open a HS popup using a CTA button in an external website. The instructions say to add class hs-cta-trigger-button hs-cta-trigger-button-... to the button.
And it works if the button is already in the page before the hubspot script is loaded, but it doesn't if the button is dynamically added to the page.
How to programmatically attach a popup to a dynamically created button?
I don't have any object HubSpotConversations. The documentation does mention an object HubSpotCallsToActions but it doesn't appear to have any method to open a popup.
You’re right—the HubSpot popup trigger (hs-cta-trigger-button) works only when the button is present before the script loads. However, for dynamically added buttons, you need to manually reinitialize the event listener.
Solution: Attach Event Listener After Button Is Added
Since your button is dynamically created, you need to use event delegation or manually re-trigger HubSpot’s script. Try this approach:
I don't have any object HubSpotConversations. The documentation does mention an object HubSpotCallsToActions but it doesn't appear to have any method to open a popup.
And thanks @ZOnlycat for the help and suggestions!
I'd love to put you in touch with some additional Top Experts to see if they have any insights to share regarding CTAs in this particular case: Hi @TomM2, @trevordjones and @danmoyle do you have ideas to help @Alberto_Gori, please?
Have a great day and thanks so much!
Best, Bérangère
Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success. Don't miss this opportunity to connect and grow—reserve your spot today!
Apreciate the tag @BérangèreL - unfortunately I'm out of my expertise on this specific question from @Alberto_Gori. I've used the pop up tool with the HubSpot tracking code on my external site, and the settings for when it shows or doesn't - not code like this. Hopefully another community member can help!
Did my answer help? Please "mark as a solution" to help others find answers. Plus I really appreciate it!