# Integrate onSubmit() into embed form Javascript

**URL:** <https://community.feathery.io/t/integrate-onsubmit-into-embed-form-javascript/904>\
**Category:** Support\
**Created:** [July 17, 2024, 8:36am UTC](https://community.feathery.io/t/integrate-onsubmit-into-embed-form-javascript/904 "2024-07-17T08:36:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amsemetrical](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@amsemetrical](https://community.feathery.io/u/amsemetrical)\
**Post date:** [July 17, 2024, 8:36am UTC](https://community.feathery.io/t/integrate-onsubmit-into-embed-form-javascript/904/1 "2024-07-17T08:36:09Z")

</div>

We want to add in the onSubmit() event handler into the embed code for the forms outlined in the documentation here - [onSubmit() | Feathery Docs](https://docs.feathery.io/develop/react/api-guide/form/event-handlers/onsubmit)

The goal is for this event to be visible in Google Tag Manager when a user completes the form, so that we can take some of the form field data and add it into the GA4 conversion event.

Here is am example of the code for a form.

`<script src="https://cdn.jsdelivr.net/npm/@feathery/react@latest/umd/index.js"></script> <div id='container'></div> <script> (() => { Feathery.init('xxxxxxxxxxxxxxxxxxx') const loginEnabled = false; Feathery.renderAt('container', { formId: "xxxxxxxx" }, loginEnabled); })(); </script>`

I’ve attempted to modify this script to include the onSubmit() functionality so I can capture the form field data, but I’m not too familiar with writing these kinds of scripts.

```auto
(() => {
    const script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/@feathery/react@latest/umd/index.js';
    script.onload = () => {
        // Create a container div for the form
        const container = document.createElement('div');
        container.id = 'container';
        document.body.appendChild(container);

        // Initialize Feathery
        Feathery.init('XXXXXXXXXXXXXXXXXXX');
        const loginEnabled = false;

        // Define the onSubmit function
        const onSubmit = (context) => {
            console.log('Submitted fields:', context.submitFields);
            if (context.lastStep) {
                console.log("User finished form!");
            }
        };

        // Render the form with onSubmit callback
        Feathery.renderAt('container', { 
            formId: "XXXXXXX",
            onSubmit: onSubmit
        }, loginEnabled);
    };
    document.head.appendChild(script);
})();

```

Can anyone help me adjust this to get the functionality I’ve described above?

---

<div class="post-metadata">

**Author:** ![eri](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@eri](https://community.feathery.io/u/eri)\
**Post date:** [July 17, 2024, 7:51pm UTC](https://community.feathery.io/t/integrate-onsubmit-into-embed-form-javascript/904/2 "2024-07-17T19:51:36Z")

</div>

Hi there, for JS embed can you try moving your onSubmit function in the form render following this format

```auto
Feathery.init('XXXXXXXXXXXXXXXXXXX');
const loginEnabled = false;

Feathery.renderAt('container', { 
    formId: "XXXXXXX",
    onSubmit: (context) => {
        //code
    },
}, loginEnabled);

```
