CMS Development

Anonymous
Non applicable

Retrieve select value from form

Résolue

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 Votes
1 Solution acceptée
tjoyce
Solution
Expert reconnu | Partenaire solutions Elite
Expert reconnu | Partenaire solutions Elite

Retrieve select value from form

Résolue

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

Voir la solution dans l'envoi d'origine

6 Réponses
dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Retrieve select value from form

Résolue

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

tjoyce
Expert reconnu | Partenaire solutions Elite
Expert reconnu | Partenaire solutions Elite

Retrieve select value from form

Résolue

Too late @dennisedson 😄

tjoyce
Solution
Expert reconnu | Partenaire solutions Elite
Expert reconnu | Partenaire solutions Elite

Retrieve select value from form

Résolue

@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
Non applicable

Retrieve select value from form

Résolue

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

dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Retrieve select value from form

Résolue

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? 

Anonymous
Non applicable

Retrieve select value from form

Résolue

Thank you so much for you assistance @dennisedson  🙌