Retrieve select value from form

Hi guys,

I’m trying to create a cookie to store a value from a select form field - I’v been trying different options but unsuccessful eveytime :sad_but_relieved_face: - can someone please help me?

The Hubspot form in question can be located: Document collaboration solutions for real-time teamwork

The select field is : Comapany Size

The value I’m trying to target is the first option: 1-10

This is my code:

<script>

var formField = document.querySelector(‘form["name=“size_of_organisation__c”]’).value;

var formValue = “1-10”;

var cookieName = sizeOfOrg;

var cookieValue = smb;

if (formField == formValue) {

document.cookie = “cookieName=cookieValue”;

}

</script>

Any help would be much appreciated

Hi @Anonymous

Unsure if you are still working on this, but wanted hook you up with some smart people that can help

@tjoyce , @Gonzalo , @BarryGrennan can you all lend a hand?

Thank you so much for you assistance @dennisedson :raising_hands:

@Anonymous - I personally would use localStorage instead of a cookie… easier to read and write

Also, you need to use the global javascript events for forms so you can run your code when the user submits the form

window.addEventListener('message', event => {
 if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormSubmit') {
 if($('[name="size_of_organisation__c"]').length){
 localStorage.setItem('organization-size', $('[name="size_of_organisation__c"]').val());
 }
 }
});

Then you can retrieve the stored item with

var orgSize = localStorage.getItem('organization-size');
console.log(orgSize)

If this answer helped, please, mark as solved :grinning_face_with_smiling_eyes:


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

Drop by and say Hi to me on slack.

@tjoyce you are a legend! Thank you so much! I really appreciate the help :folded_hands:

don’t let it go to your head, @tjoyce :winking_face_with_tongue:

Too late @dennisedson :grinning_face_with_smiling_eyes: