October 13, 2021 at 11:02 am | Programming, web.

Getting values from the hubspot form in it’s onFormSubmit handler can be pretty tricky so here is a quick tip.

Add the onFormSubmit closure function to the hbspt.forms.create script and then use .find() to get the value of individual form elements that you need. At that point you can use them to pass to tracking or cookies or whatever.

<script>
  hbspt.forms.create({
	region: "na1",
	portalId: "555121212",
	formId: "23845959d-1d80-4af7-81ae-5b3f5c52asf888",
	onFormSubmit: function($form) {

		var firstname = $form.find("[name=firstname]").val();
		var lastname = $form.find("[name=lastname]").val();
		var email = $form.find("[name=email]").val();
		var jobtitle = $form.find("[name=jobtitle]").val();
		var company = $form.find("[name=company]").val();
		
	} 
});
</script>