Hubspot form: programmatically set field value via jQuery


We are using the hubspot provided jquery :frowning:
I think the problem is a little different, because we have no problems in notifying Hubspot normal changes. We have problems in notifying hubspot the first set-up of the fields.
This is necessary because Hubspot automatically selects the old values from the user, but does not notify the changes with any event. We need to reset the form due to this, because Hubspot is slower than our script. So, when we set values before hubspot set its ones, these are overwritten.


I see the bug fixed in one of the instance. But need to confirm if it’s fixed universally for every hubspot instance.

Will post back once I’m assured. :slight_smile:


We got on hubspot forms with custom validation too, I hope this might also be intresting for you :slight_smile:

Hubspot COS, forms with custom validation



I have tried to based on your code. It’s not working myside. My below code is,

portalId: '3042190',
formId: '2aa8ad7b-81a7-449e-8318-48cf1c26f4b8',
 onFormSubmit: function($form) {   		


Please let me know, what I missed that.



Hello guys,

I’m having some problems with forms and its values, I know it’s not the same thing discussed here but I’d like to know if there’s any way that we can achieve what we need.

The story is the following:

So basically, we’ve built custom private pages for our client’s customers. The thing is that there are many customers groups/segments and we created one private page per group.

So, in order to join these pages, the users must fill out a form inside HubSpot and one of the form fields is called “Security Token”, a single-line text field.

Depending on the group they belong to, the customers will enter a different “Security Token”, with this “token”, we identify the segment where they belong to.

So, for internal purposes, we need to pass the “Security Token” field value into a string parameter in the URL of the page that they’re redirected.

An example:

  1. The user fills out the form (COS Page and HubSpot form), and its token is “TOKEN101”
  2. Form is submitted and it’s redirected to the desination page.
  3. The destination page URL should be “

Is there any way to achieve this? Maybe using Forms API?

Thanks in advance for your time :slight_smile:


Hi, with this function you can append the submitted values to the redirect url. Alternatively you can modify it in order to pass only the “token” field.
Hope this helps.


Thanks for tthe help Damiano! I appreciate it so much :slight_smile:

So, I tried with the following code, but it’s not working at the moment

function appendFormData (form) {

  var redirect = "";

  var appendFields = function (url, values) {
    var data = {};
    $.each(values, function (i, item) {
      if ( !== "token_de_seguridad") {
        data[] = item.value;
    if (url.match(/\?/)) {
      return url + "&" + $.param(data);
    } else {
      return url + "?" + $.param(data);

  var $hsContext = form.find("input[name='token_de_seguridad']");
  var hsContext = JSON.parse($hsContext.val());
  hsContext.redirectUrl = appendFields(redirect, form.serializeArray());

Also, I added this to the hubspot embed code:

onFormSubmit : function($form, ctx) {

I modified it in order to pull only the data we need but I think I’m missing out something. With these changes, the form doesn’t redirect, it works because the data is sent, but the redirect doesn’t happen so I think it’s something related to the variables using the parameter “redirectUrl”

Any ideas?

Thanks again for the help

Warm regards!


For anyone who finds this via Google Search - the answer is that form elements need to be manipulated with .change() detailed here:

This won’t work:

This will:


How do we do this with vanilla JS (no JQuery)? Or can someone link me to the answer if it’s already been given elsewhere?


Hi guys and gals,

I had the same problem and fixed it by using the script in a different scope “jQuery” instead of “$”.


Not working:


I have the same issue, I'm trying to write on an input:
but it doesn't:disappointed_relieved: work.


Hi @Payman_Ferdowsali,

Is the form fully loaded before that script runs? Have you tried replacing $ with jQuery as mentioned above?


I used Document.load method from Javascrip to make sure it is loaded. I also both $ and JQuery. Apparantly the Document is loaded, but doesnt have all HTML5 attributes!


Hi @Payman_Ferdowsali,

The Document.load method isn't the best event for this; that event fires when the DOM is loaded, but HubSpot forms are loaded dynamically by the form embed code. You should put the form field related code in the built-in onFormReady callback:


Hi @Derek_Gervais,

I did so, but when I use onFormReady method, I get the error message saying:

shell.js:9 The onFormReady function in hbspt.forms.create requires jQuery. It was not run.

This is the code I have:

portalId: '383XXXX',
formId: '6ae5c91f-67cf-40e7-bc53-XXXXXXXXXXXX',
onFormReady: function($form){


Hi @Payman_Ferdowsali,

The error is accurate here; you need to include jQuery to get the full functionality of the form embed code. jQuery is included on HubSpot pages by default, but may not be included on yours.


I included Jquery, but still getting the same error. This is my code:

<script src="jquery-3.3.1.min.js"></script>
<script charset="utf-8" type="text/javascript" src="//"></script>
portalId: '383xxxx',
formId: '6ae5c91f-67cf-40e7-bc53-14bbxxxxxxxx',      
onFormReady: function($form){
document.getElementById("firstname-6ae5c91f-67cfxxxxxxxxxxxxx").value = "Brian";  


and this is the error I get:

The onFormReady function in hbspt.forms.create requires jQuery. It was not run.


I found the solution:
1- I added jquery v 1.12.4 before hobspot form gets loaded.
2- Instead of using the which is in the default embedded code by hubspot, I used


Someone from this thread can look into this one Cannot read property 'target' of undefined from current.js please



I'm trying to set the email field of my form but I have the same problem as OP. I'm not using jQuery, I'm using vanilla JS using a global event(onFormReady) like what is documented in this page:

Inside it I have something like this:

document.getElementById("email-id").value = ""

And it works, the value is set into the email field, but it dissapears after focusing on other fields. Is there any way of solving this?
I tried to launch onchange(), but didn't work.