# Javascript Cookie Values passed into Hubspot Form

**URL:** <https://community.hubspot.com/t/javascript-cookie-values-passed-into-hubspot-form/15446>\
**Category:** APIs & Integrations\
**Tags:** forms\
**Created:** [July 23, 2019, 1:06pm UTC](https://community.hubspot.com/t/javascript-cookie-values-passed-into-hubspot-form/15446 "2019-07-23T13:06:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobinSwapcard](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/robinswapcard/32/26012_2.png) [@RobinSwapcard](https://community.hubspot.com/u/RobinSwapcard)\
**Post date:** [July 23, 2019, 1:06pm UTC](https://community.hubspot.com/t/javascript-cookie-values-passed-into-hubspot-form/15446/1 "2019-07-23T13:06:43Z")

</div>

Hi,

I’m trying to prefill my form with data from cookie. I do not use jquery.

```auto
 window.addEventListener("message", event => {
 if (
 event.data.eventName === "onFormReady" &&
 event.data.type === "hsFormCallback"
 ) {
 var event = new Event("change");
 var evt = document.createEvent("HTMLEvents");
 evt.initEvent("change", false, true);

 document.getElementsByName("marketing_lead_source")[0].value =
 "robin";

 document
 .getElementsByName("marketing_lead_source")[0]
 .dispatchEvent(event);

 console.log(
 document.getElementsByName("marketing_lead_source")[0].value
 );
 }
 });

```

Nothing works 😞

---

<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:** [July 24, 2019, 3:35am UTC](https://community.hubspot.com/t/javascript-cookie-values-passed-into-hubspot-form/15446/2 "2019-07-24T03:35:07Z")

</div>

Hi @RobinSwapcard,

When looking at the codes you provided do you mind clarifying where are you getting the cookies and using the cookie to prefill the form?

Currently, it seems that you’re setting the form field lead source to _robin_, can I check if that prefilled the form field to robin?

Ultimately, you’d like to use the cookie you retrieve to populate on the HubSpot form, am I right?

In this case, do you mind sharing with me the page that the form is on and the function you wrote to get the cookies?

---

<div class="post-metadata">

**Author:** ![RobinSwapcard](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/robinswapcard/32/26012_2.png) [@RobinSwapcard](https://community.hubspot.com/u/RobinSwapcard)\
**Post date:** [July 25, 2019, 8:00am UTC](https://community.hubspot.com/t/javascript-cookie-values-passed-into-hubspot-form/15446/3 "2019-07-25T08:00:18Z")

</div>

Hello,

The cookie is not the issue. Right now I’m not able to set the source as “robin”

> **[Schedule a Live Demo and Experience Swapcard’s Platform](https://www.swapcard.com/contact-us)**
>
> Get a custom demo to explore how Swapcard's intuitive platform and AI-powered capabilities help enhance attendee engagement and exhibitor ROI, and grow revenue.

---

<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:** [July 26, 2019, 3:05am UTC](https://community.hubspot.com/t/javascript-cookie-values-passed-into-hubspot-form/15446/4 "2019-07-26T03:05:34Z")

</div>

Hi @RobinSwapcard,

Digging into this, the best way to implement this is through the onFormSubmit callback and this would require jquery (I understand that you mention you ain’t using jquery now, is there a reason why?)

If you’d like an example of a working code, you can refer to the following codes:

```auto
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>

<body>
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
 hbspt.forms.create({
	portalId: "{{portal_id}}",
	formId: "{{form_id}}",

	onFormSubmit: function($form, ctx){
		document.getElementById("{{the marketing lead source form id}}").value = "Robin";
	}

});
</script>

</body>
</html>

```

Hope this helps!
