# Getting form field on form submit

**URL:** <https://community.hubspot.com/t/getting-form-field-on-form-submit/18581>\
**Category:** APIs & Integrations\
**Tags:** forms\
**Created:** [February 5, 2020, 4:24pm UTC](https://community.hubspot.com/t/getting-form-field-on-form-submit/18581 "2020-02-05T16:24:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![solsen](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/solsen/32/9140_2.png) [@solsen](https://community.hubspot.com/u/solsen)\
**Post date:** [February 5, 2020, 4:24pm UTC](https://community.hubspot.com/t/getting-form-field-on-form-submit/18581/1 "2020-02-05T16:24:44Z")

</div>

Hi all,

I’m new to Javascript but want to do the following: on form submit, if a certain field in my form is “No”, redirect them to a Stripe checkout page. I have it working with onFormSubmit, but I don’t know how to get the field so that I can show the checkout page conditionally based on the value of the form field. Here is what I have so far:

```auto
<script>
	window.addEventListener('message', event => {
	 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
	 callStripe('my_sku');
	 }
	});
</script>

```

From here I would just want to add an if statement along the lines of "if(formFieldValue === ‘No’) { callStripe(); }. How can I grab the value of a form field for the form that has just been submitted?

Thanks!

---

<div class="post-metadata">

**Author:** ![WendyGoh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wendygoh/32/42320_2.png) [@WendyGoh](https://community.hubspot.com/u/WendyGoh)\
**Post date:** [February 6, 2020, 3:24am UTC](https://community.hubspot.com/t/getting-form-field-on-form-submit/18581/2 "2020-02-06T03:24:43Z")

</div>

Hey @solsen,

When looking to grab the value of a form field from the form, you can use the following code:

`$('input[value="{{formFieldValue}}"]')`

You can learn more about this on our documentation - [Manipulating forms with jQuery](https://developers.hubspot.com/manipulating-forms-with-jquery).

---

<div class="post-metadata">

**Author:** ![solsen](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/solsen/32/9140_2.png) [@solsen](https://community.hubspot.com/u/solsen)\
**Post date:** [February 6, 2020, 7:44pm UTC](https://community.hubspot.com/t/getting-form-field-on-form-submit/18581/3 "2020-02-06T19:44:37Z")

</div>

Hi @WendyGoh - thanks for the quick response. I think I am close, but could you let me know what I’m doing wrong when testing for the field’s value here?

```auto
<script>
	hbspt.forms.create({
		portalId: "my_portal_id",
		formId: "my_form_id",
		onFormSubmit: function($form){
			if($('input[value="my_field_name"]') === 'No'){
				callStripe();
			}
		}
	});
</script>

```

---

<div class="post-metadata">

**Author:** ![WendyGoh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/wendygoh/32/42320_2.png) [@WendyGoh](https://community.hubspot.com/u/WendyGoh)\
**Post date:** [February 7, 2020, 1:47am UTC](https://community.hubspot.com/t/getting-form-field-on-form-submit/18581/4 "2020-02-07T01:47:30Z")

</div>

Hey @solsen,

Ahh, I believe we would need to include _.val()_

```auto
<script>
	hbspt.forms.create({
		portalId: "my_portal_id",
		formId: "my_form_id",
		onFormSubmit: function($form){
			if($('input[value="my_field_name"]').val() === 'No'){
				callStripe();
			}
		}
	});
</script>

```

I have tested this out on my end using the following code:

```auto
 <script>
 hbspt.forms.create({
 css: '',
 portalId: '{{myPortalId}}',
 formId: '{{myFormId}}',
 onFormSubmit: function($form) {
 if( $('input[name="stringtest"]').val() === 'test' ) {
 alert('hello');
 $('input[name="stringtest"]').val("kitkat2").change();
 }
 }
 });
 </script>

```

This works just fine. Could you try and see if this works for you?

---

<div class="post-metadata">

**Author:** ![solsen](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/solsen/32/9140_2.png) [@solsen](https://community.hubspot.com/u/solsen)\
**Post date:** [February 7, 2020, 6:48am UTC](https://community.hubspot.com/t/getting-form-field-on-form-submit/18581/5 "2020-02-07T06:48:55Z")

</div>

Hi @WendyGoh - thanks for the follow-up! Strangely enough, I was not able to get the values for any of my form fields that way - they all simply returned “undefined”. I found a workaround using the .serialize() function:

```auto
hbspt.forms.create({
	portalId: "my_portal_id",
	formId: "my_form_id",
	onFormSubmit: function($form){
		var formData = $form.serialize();
		var fieldName = "my_field_name";
		var fieldPosition = formData.search(fieldName);

		if(formData[fieldPosition + fieldName.length+1] === 'N'){ //for "No"
			callStripe();
		}
		else{
			window.open("other_page");
		}
	}
});

```

Thanks for your help!
