Skip to main content


Ever needed a reliable way to fire some JavaScript on form submission?


This solution will be super handy for anybody looking to do things like: manipulating form data before it’s submitted or making custom API calls or XHR requests on form submission.


Whatever custom JavaScript you may have, using this script is a reliable and easy way to ensure it is fired on form submission.


Give’r a whirl! :spinbounce:




How to Install in Unbounce




Click Here for Instructions




🚨

This is not an official Unbounce feature. This functionality is based entirely on third party code, and has only been tested in limited applications. Since this isn’t a supported Unbounce feature, our support team will be unable to assist if things go awry. So if you are not comfortable with HTML, Javascript and CSS, please consider consulting an experienced developer.





Documentation


There are two options for when your code will run:




  • beforeFormSubmit: Called after validation has passed, but before the lead has been recorded. This could be useful to manipulate the form data before it’s submitted.


  • afterFormSubmit: Called after the lead has been recorded. This could be useful to make an API call or show some kind of additional ‘thank you’ message to the lead.


Asynchronous hooks are supported: your function can return a promise, or it can take a callback as its second argument and call it when an asynchronous action has completed. This allows you to delay the next part of form submission until an asynchronous action (e.g. a fetch or XHR request) has completed. However, this is not generally recommended, because slowing down the form submit process could cause you to lose leads.


The functions will be called with an object as the first argument, containing the following properties:




  • formElement: The form’s DOM element


  • isConversionGoal: A boolean representing whether form submission was enabled as a conversion goal


Note: if you register multiple beforeFormSubmit and/or afterFormSubmit hooks, they will be called in parallel.


Examples


Here are some examples. You can use these as starting points to write your own hooks.


Example 1: update a form field value (trim spaces) before submit


<script>
window.ub.hooks.beforeFormSubmit.push(function(args) {
var input = args.formElement.querySelector('input#name');
input.value = input.value.trim();
});
</script>

Example 2: show a message after submit


<script>
window.ub.hooks.afterFormSubmit.push(function() {
alert('Thank you!');
});
</script>

Example 3: asynchronously update a form field value from an external API before submit


Note: this example uses fetch which is not supported in Internet Explorer


<script>
window.ub.hooks.beforeFormSubmit.push(function(args) {
return fetch('https://reqres.in/api/users')
.then(function(response) {
return response.json();
})
.then(function(body) {
return body.datat0].first_name + ' ' + body.data 0].last_name;
})
.catch(function() {
return 'Unknown';
})
.then(function(name) {
args.formElement.querySelector('#name').value = name;
});
});
</script>





Can’t see the instructions? Log-in or Join the Community to get access immediately. 🚀





Want to take your Unbounce landing pages + Convertables™️ to the next level?

:spinbounce: Check out the Ultimate List of Unbounce Tips, Scripts & Hacks

Not sure about the data layer question - it’s just an array of stuff i can push into and it resets on every page load.


But in effect i set up the workflow as follows:



  1. Tag 1: Above script runs on all pages. So both functions get registered with the unbounce hooks.

  2. New Trigger: Custom event: ub-form-success

  3. New data layer variables for fields i care about e.g email => {{Form Submission - Email}} which references the key i set up on the datalayer => ‘form-email’

  4. Tag 2: Triggered on ‘ub-form-success’ and the script just constructs the event properties I need to send to our cdp. afterFormSubmit runs before the page reloads / is redirected so I have enough time to grab what i need from datalayer and send over.


But yeah, :+3: for documentation comment


Hey @Richard_Makara , was wondering if you think I could use your method here to solve my problem




I’m don’t come from a coding background so would love to hear your thoughts.


Hi all,

This line is not compatible with Smart Builder:

var email = args.formElement.querySelector('input#email');

Replace it with this one to use on both Classic and Smart Builder pages:

var email = document.querySelector('input#email, input#emailField'); 

 


Reply