Typeform Close Event | Community
Skip to main content
Answered

Typeform Close Event

  • February 25, 2022
  • 5 replies
  • 399 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
Community Team
Forum|alt.badge.img+5
  • Tech Community Advocate
  • 14984 replies
  • February 25, 2022

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
  • 2 replies
  • February 25, 2022

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


picsoung
Typeform
Forum|alt.badge.img+5
  • Developer Advocate @ Typeform
  • 390 replies
  • Answer
  • February 26, 2022

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
  • 2 replies
  • February 26, 2022

@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
  • 888 replies
  • March 7, 2022

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