Skip to main content
Answered

onSubmit function for multiple typeform on one page

  • December 20, 2021
  • 2 replies
  • 113 views

0

 

I am embedding 2 typeform on my webpage. When I submit 1st typefrom onSubmit function get executed, but onSubmit function of 2nd typeform get called automatically even I submitted the 1st typeform.

1<div id="my-embedded-typeform1" style="width: 100%; height: 300px;"></div>
2<div id="my-embedded-typeform2" style="width: 100%; height: 300px;"></div>
3
4<script src="https://embed.typeform.com/embed.js" type="text/javascript"</script>
5 window.addEventListener("DOMContentLoaded", function() {
6 var el = document.getElementById("my-embedded-typeform1");
7 var el2 = document.getElementById("my-embedded-typeform2");
8 window.typeformEmbed.makeWidget(el1, "https://admin.typeform.com/to/cVa5IG", {
9 hideFooter: true,
10 onSubmit: () => { alert('Typeform 1 submitted') }
11 });
12window.typeformEmbed.makeWidget(el2, "https://admin.typeform.com/to/cVa5IG", {
13 hideFooter: true,
14 onSubmit: () => { alert('Typeform 2 submitted') }
15 });
16 });

Best answer by mathio-tf

Hello @wellinston37247 

you have a JavaScript error in your code:

Uncaught ReferenceError: el1 is not defined

Also the <script> tag is missing bracket >.

 

Also, you are using old version of embed library. While this library is still available, we no longer provide support for it. I suggest you use latest embed library - version 1.

Here is your code rewritten for new library:

1<div id="my-embedded-typeform1" style="width: 100%; height: 300px;"></div>
2<div id="my-embedded-typeform2" style="width: 100%; height: 300px;"></div>
3
4<script src="//embed.typeform.com/next/embed.js" type="text/javascript"></script>
5<script>
6 window.addEventListener("DOMContentLoaded", function() {
7 var el = document.getElementById("my-embedded-typeform1");
8 var el2 = document.getElementById("my-embedded-typeform2");
9
10 window.tf.createWidget('cVa5IG', {
11 container: el,
12 hideFooter: true,
13 onSubmit: () => { alert('Typeform 1 submitted') }
14 });
15
16
17 window.tf.createWidget('cVa5IG', {
18 container: el2,
19 hideFooter: true,
20 onSubmit: () => { alert('Typeform 2 submitted') }
21 });
22});
23</script>
24

I tested the code above and it works as expected.

View original

2 replies

mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • 888 replies
  • Answer
  • December 20, 2021

Hello @wellinston37247 

you have a JavaScript error in your code:

Uncaught ReferenceError: el1 is not defined

Also the <script> tag is missing bracket >.

 

Also, you are using old version of embed library. While this library is still available, we no longer provide support for it. I suggest you use latest embed library - version 1.

Here is your code rewritten for new library:

1<div id="my-embedded-typeform1" style="width: 100%; height: 300px;"></div>
2<div id="my-embedded-typeform2" style="width: 100%; height: 300px;"></div>
3
4<script src="//embed.typeform.com/next/embed.js" type="text/javascript"></script>
5<script>
6 window.addEventListener("DOMContentLoaded", function() {
7 var el = document.getElementById("my-embedded-typeform1");
8 var el2 = document.getElementById("my-embedded-typeform2");
9
10 window.tf.createWidget('cVa5IG', {
11 container: el,
12 hideFooter: true,
13 onSubmit: () => { alert('Typeform 1 submitted') }
14 });
15
16
17 window.tf.createWidget('cVa5IG', {
18 container: el2,
19 hideFooter: true,
20 onSubmit: () => { alert('Typeform 2 submitted') }
21 });
22});
23</script>
24

I tested the code above and it works as expected.


Liz
Ex–Typefomer
Forum|alt.badge.img+5
  • Tech Community Advocate
  • 15035 replies
  • December 28, 2021

Hi @wellinston37247 Were you able to use @mathio ‘s suggestion to fix the issue? :grinning:


Reply