Answered

Typeform Close Event

  • 25 February 2022
  • 5 replies
  • 225 views

You have autoclose option which fires onClose callback after certain seconds of onSubmit event but is there a way that I can fire onClose event on some successful api request. For more clairty the scenario is - 
when I click on submit button, some api request is made then on successfull response I want to close my typeform 

icon

Best answer by picsoung 26 February 2022, 01:28

View original

5 replies

Userlevel 7
Badge +5

Hi @mohit512611 I don’t think there is an option for this, but tagging @mathio in case he happens to know any workarounds. 

@mathio If you can give a solution around this, it would be great

Userlevel 7
Badge +5

Hi @mohit512611 

If you want to close the form automatically after it has been submitted you can use the autoClose option as described in our documentation here.

Once the form is submitted you can’t really go back and display an error in the form. In case you wanted to display an error if the API call failed.

So you probably need to make the API request in the onSubmit callback.

@picsoung Hi, thanks for your time and reply, yes you are right I am making the API request in onSubmit callback only but I want to close the typeform once my api succeeds or fails, I do not want to show any error msg, I just want to sync the closing of my typeform with my API request response.

Userlevel 7
Badge +5

Hello @mohit512611 

you can close your typeform “manually” based on your API call response like this:

<button id="button">open form</button>
<script src="//embed.typeform.com/next/embed.js"></script>
<link rel="stylesheet" href="//embed.typeform.com/next/css/popup.css" />
<script>
const { open, close } = window.tf.createPopup('<form-id>', {
onSubmit: async () => {
const response = await fetch('/my/endpoint')
if (response.ok) {
close()
} else {
alert('Failed')
}
}
})
document.querySelector('#button').onclick = open
</script>

Note: This will leave your respondent on your form ending. The form will be submitted regardless of your API call.

Reply