Skip to main content
Answered

Typeform Close Event

  • Feb 25, 2022 11:42 AM UTC
  • 5 replies
  • 528 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 

Best answer by picsoung

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.

View original

5 replies

Liz
Ex–Typefomer
Forum|alt.badge.img+5
  • Tech Community Advocate
  • Feb 25, 2022 5:10 PM UTC

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


  • Author
  • Explorer
  • Feb 25, 2022 5:15 PM UTC

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


picsoung
Typeform
Forum|alt.badge.img+5
  • Developer Advocate @ Typeform
  • Answer
  • Feb 26, 2022 12:28 AM UTC

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.


  • Author
  • Explorer
  • Feb 26, 2022 2:59 AM UTC

@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.


mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • Mar 7, 2022 2:07 PM UTC

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.