# Form handlers

**URL:** <https://community.hubspot.com/t/form-handlers/7632>\
**Category:** CMS Development\
**Created:** [February 2, 2018, 4:48am UTC](https://community.hubspot.com/t/form-handlers/7632 "2018-02-02T04:48:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ironshield](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ironshield/32/63860_2.png) [@ironshield](https://community.hubspot.com/u/ironshield)\
**Post date:** [February 2, 2018, 4:48am UTC](https://community.hubspot.com/t/form-handlers/7632/1 "2018-02-02T04:48:40Z")

</div>

What is the best practice for adding JS code to form submissions? If I add a form to a HubSpot page, how can I hook into the form’s submit event?

Specifically, I’m trying to add a Facebook pixel event to a form submission. It works intermittently if I add the JS snippet to the “inline thank you message.” But it’s just not consistent.

---

<div class="post-metadata">

**Author:** ![anders\_grove](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anders_grove/32/99626_2.png) [@anders\_grove](https://community.hubspot.com/u/anders_grove)\
**Post date:** [February 2, 2018, 8:19am UTC](https://community.hubspot.com/t/form-handlers/7632/2 "2018-02-02T08:19:35Z")

</div>

You can use the embed code and add the _onFormSubmit_ option:

```auto
hbspt.forms.create({
 portalId: 'xxxxx',
 formId: 'xxxxx-xxxx-xxxx-xxxx-xxxx',
 onFormSubmit: function($form) {
 // YOUR SCRIPT HERE
 } 
}); 

```

---

<div class="post-metadata">

**Author:** ![TRooInbound](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/trooinbound/32/89545_2.png) [@TRooInbound](https://community.hubspot.com/u/TRooInbound)\
**Post date:** [February 2, 2018, 12:24pm UTC](https://community.hubspot.com/t/form-handlers/7632/3 "2018-02-02T12:24:25Z")

</div>

Hi @ironshield,

We are agree with answer given by [@anders\_grove](https://community.hubspot.com/t5/user/viewprofilepage/user-id/52525).

We just want to add some more details to that answer

“onFormSubmit” attribute is used to provide callback funtion that executes after form is validated, just before the data is actually sent.

for more information regarding form embed code please visit [here](https://developers.hubspot.com/docs/methods/forms/advanced_form_options).

Thanks,

Team TRooInbound

---

<div class="post-metadata">

**Author:** ![ironshield](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ironshield/32/63860_2.png) [@ironshield](https://community.hubspot.com/u/ironshield)\
**Post date:** [February 2, 2018, 8:43pm UTC](https://community.hubspot.com/t/form-handlers/7632/4 "2018-02-02T20:43:25Z")

</div>

Is it possible to use this technique within the “Design Manager,” using the Form module? Or do I have to embed the form code using the Custom HTML module?

---

<div class="post-metadata">

**Author:** ![anders\_grove](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anders_grove/32/99626_2.png) [@anders\_grove](https://community.hubspot.com/u/anders_grove)\
**Post date:** [February 5, 2018, 10:27am UTC](https://community.hubspot.com/t/form-handlers/7632/5 "2018-02-05T10:27:59Z")

</div>

Hey @ironshield,

An alternative method is to have the form redirect to a thank you page and add the facebook pixel event code to that page.

The _onFormSubmit_ method is only possible by using embedded forms as it’s part of hbspt.forms.create() call.

So if you have to have your script fire at a specific time, or if you have to stay on the same page I’d suggest use embedded forms. There are some jquery techniques you could try, but they’re usually buggy and inconsistent. Otherwise add it to you thank your page.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![ironshield](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ironshield/32/63860_2.png) [@ironshield](https://community.hubspot.com/u/ironshield)\
**Post date:** [February 6, 2018, 1:12am UTC](https://community.hubspot.com/t/form-handlers/7632/6 "2018-02-06T01:12:13Z")

</div>

Thanks [@anders\_grove](https://community.hubspot.com/t5/user/viewprofilepage/user-id/52525)

That’s what I was afraid of. I appreciate the confirmation.
