I have a react application with an embedded form from cognito forms. The script I have below embeds the form in my component. However, I want to add an onSubmit
function within the component for that form. I tried to simply add onSubmit
to the iframe
but that does not do anything.
According to their docs, you are supposed to add a script
:
<script>
Cognito.on('beforeSubmit', function(event) {
if (event.data.entry.Name.First === 'Bob')
event.preventDefault();
});
</script>
But I have no idea how to implement that in React. I get the error 'Cognito.on' is not a function
. Here is the code I have embedding the form:
{indJob &&
<iframe src={indJob.forms.electrical}></iframe>
<script src="https://www.cognitoforms.com/f/iframe.js"></script>
}
Iframe has it’s own separate context and browser doesn’t allow parent application and Iframe to share stuff directly. So in your case try rendering the
cognitoform's
script inside your Iframe source code on top level.