# How can we trigger a JS function when a CTA form is submitted?

**URL:** <https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195>\
**Category:** CMS Development\
**Tags:** professional\
**Created:** [July 26, 2023, 4:34pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195 "2023-07-26T16:34:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HJavaid](https://avatars.discourse-cdn.com/v4/letter/h/c68b51/32.png) [@HJavaid](https://community.hubspot.com/u/HJavaid)\
**Post date:** [July 26, 2023, 4:34pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195/1 "2023-07-26T16:34:40Z")

</div>

I have a lead capture CTA popup with a form added on my website, How can I trigger a JS function when a form is submitted within CTA popup?

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [July 27, 2023, 7:41pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195/2 "2023-07-27T19:41:59Z")

</div>

Hi, @HJavaid 👋 Welcome to the community. Thanks for your question.

- Can you give the community a bit more info about your project, please?
- Is your CTA pop-up and form HubSpot created? Or custom coded?

If a HubSpot Form, and you have Marketing or CMS Hub Professional or Enterprise, you can set the form to raw HTML and [edit the Form Embed Code](https://legacydocs.hubspot.com/docs/methods/forms/advanced_form_options).

Here is the Global Form Events documentation — [Using global form events](https://legacydocs.hubspot.com/global-form-events)

Form API — [Submit a Form with Authentication](https://legacydocs.hubspot.com/docs/methods/forms/submit_form_v3_authentication)

Hey, @alyssamwilie @daveroma @himanshurauthan, do you have an example you can share with @HJavaid?

Thank you very much! — Jaycee

---

<div class="post-metadata">

**Author:** ![HJavaid](https://avatars.discourse-cdn.com/v4/letter/h/c68b51/32.png) [@HJavaid](https://community.hubspot.com/u/HJavaid)\
**Post date:** [July 27, 2023, 8:44pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195/3 "2023-07-27T20:44:00Z")

</div>

Hi [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074)  
I am using hubspot lead capture CTA, it is not custom coded.

---

<div class="post-metadata">

**Author:** ![daveroma](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/daveroma/32/82727_2.png) [@daveroma](https://community.hubspot.com/u/daveroma)\
**Post date:** [July 27, 2023, 7:59pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195/4 "2023-07-27T19:59:24Z")

</div>

Hey [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074) & @HJavaid

In order to trigger a JavaScript function when a form is submitted in HubSpot, you can leverage HubSpot’s onFormSubmit callback function.

Although these are based on [the legacy documentation](https://legacydocs.hubspot.com/docs/methods/forms/advanced_form_options), I believe they are still applicable and can help address your need. The onFormSubmit callback function executes right after the form validation step and just before the form data is actually sent or submitted. This function is ideal for any logic that you’d like to run before the form submission begins. Please note, any changes made to form fields at this stage will not be validated.

Here’s an example of how to use the onFormSubmit callback:

```lia-code-sample
hbspt.forms.create({
 portalId: 'your_portal_id',
 formId: 'your_form_id',
 onFormSubmit: function($form) {
 // Insert your JavaScript logic here
 }
});

```

In this snippet, replace ‘your\_portal\_id’ and ‘your\_form\_id’ with your actual HubSpot portal ID and form ID respectively. Within the onFormSubmit function, you can add your custom JavaScript logic that you’d like to execute prior to the form submission.

Dave Roma

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [July 27, 2023, 8:38pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195/5 "2023-07-27T20:38:15Z")

</div>

Thank you, @daveroma 🙌 — Jaycee

---

<div class="post-metadata">

**Author:** ![HJavaid](https://avatars.discourse-cdn.com/v4/letter/h/c68b51/32.png) [@HJavaid](https://community.hubspot.com/u/HJavaid)\
**Post date:** [July 27, 2023, 8:42pm UTC](https://community.hubspot.com/t/how-can-we-trigger-a-js-function-when-a-cta-form-is-submitted/86195/6 "2023-07-27T20:42:19Z")

</div>

@daveroma I have already tried this thing, it creates a new form on page and don’t fire on form submission when the form on lead capture CTA is submitted
