system
November 20, 2020, 8:42am
1
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: 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?
system
November 30, 2020, 8:55am
3
Thank you so much for you assistance @dennisedson
tjoyce
November 24, 2020, 8:59am
4
@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 .
system
November 30, 2020, 8:57am
5
@tjoyce you are a legend! Thank you so much! I really appreciate the help
don’t let it go to your head, @tjoyce