CMS Development

Anonymous
Not applicable

Retrieve select value from form

SOLVE

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 😥 - can someone please help me?

 

The Hubspot form in question can be located: https://www.huddle.com/get-started

 

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

0 Upvotes
1 Accepted solution
tjoyce
Solution
Recognized Expert | Platinum Partner
Recognized Expert | Platinum Partner

Retrieve select value from form

SOLVE

@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 😄


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

 

Drop by and say Hi to me on slack.

View solution in original post

6 Replies 6
dennisedson
HubSpot Product Team
HubSpot Product Team

Retrieve select value from form

SOLVE

don't let it go to your head, @tjoyce 😜

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
tjoyce
Recognized Expert | Platinum Partner
Recognized Expert | Platinum Partner

Retrieve select value from form

SOLVE

Too late @dennisedson 😄

tjoyce
Solution
Recognized Expert | Platinum Partner
Recognized Expert | Platinum Partner

Retrieve select value from form

SOLVE

@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 😄


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

 

Drop by and say Hi to me on slack.

Anonymous
Not applicable

Retrieve select value from form

SOLVE

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

dennisedson
HubSpot Product Team
HubSpot Product Team

Retrieve select value from form

SOLVE

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? 

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
Anonymous
Not applicable

Retrieve select value from form

SOLVE

Thank you so much for you assistance @dennisedson  🙌