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:
// Function to rebind the HubSpot CTA trigger
function rebindHubSpotCTA() {
setTimeout(() => {
const buttons = document.querySelectorAll(‘.hs-cta-trigger-button’);
buttons.forEach(button => {
button.addEventListener(‘click’, function() {
// Trigger the HubSpot popup manually
window.HubSpotConversations.widget.open();
});
});
}, 500); // Delay to ensure the button is in the DOM
}
// Example: Dynamically creating a button
const newButton = document.createElement(‘button’);
newButton.className = ‘hs-cta-trigger-button’;
newButton.textContent = ‘Open Popup’;
document.body.appendChild(newButton);
// Call the function to rebind
rebindHubSpotCTA();
Why This Works?
Ensures the button exists before attaching the event listener.
Uses a delay to allow HubSpot scripts to load properly.
Manually binds the click event to trigger the popup.
If you’re using jQuery, you can also try:
$(document).on(‘click’, ‘.hs-cta-trigger-button’, function() {
window.HubSpotConversations.widget.open();
});