<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Setting form field values with javascript in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762582#M33052</link>
    <description>&lt;P&gt;hey &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/103660"&gt;@piersg&lt;/a&gt;, I tried that as well unfortunately it doesn't trigger the validation at all (and after the form submission it again clears the input). It seems that the react form fields only listen to the input event. I also tried it with jquery as it was mentioned in other posts but without success:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;$(companyfield).val("MyCompanyName").change();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2023 12:44:26 GMT</pubDate>
    <dc:creator>paradummy</dc:creator>
    <dc:date>2023-03-01T12:44:26Z</dc:date>
    <item>
      <title>Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762173#M33033</link>
      <description>&lt;P&gt;Hi folks&lt;/P&gt;&lt;P&gt;I'm trying to set the values for some form fields of a hubspot form with javascript. But the values gets deleted after validation (or in this case after input/change event). I remember that this used to work (and it's also the solutions in a lot of other posts). Has anyone an idea what could be the problem? Or am I doing something wrong?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;window.addEventListener('message', event =&amp;gt; {
  
  const formId = "MY_FORM_ID";
  
  if(event.data.type === 'hsFormCallback' &amp;amp;&amp;amp; event.data.eventName === 'onFormReady' &amp;amp;&amp;amp; event.data.id === formId) {
    
    const form = document.querySelector(`[data-form-id="${event.data.id}"]`);
    
    // doesn't work - triggers validation without setting the value
    const emailfield = form.elements.email;
    emailfield.value = "myemail@address.com";
    emailfield.dispatchEvent(new Event('input', { 'bubbles': true }));
    
    const companyfield = form.elements.company;
    companyfield.value = "MyCompanyName";
    companyfield.dispatchEvent(new Event('input', { 'bubbles': true }));
    
    const adressfield = form.elements.address;
    adressfield.value = "MyAddress";
    adressfield.dispatchEvent(new Event('input', { 'bubbles': true }));
    
    // doesn't work the jquery way either
    //$(companyfield).val("MyCompanyName").change();
    
  }
  
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 17:15:38 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762173#M33033</guid>
      <dc:creator>paradummy</dc:creator>
      <dc:date>2023-02-28T17:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762228#M33038</link>
      <description>&lt;P&gt;Have you tried it with the &lt;CODE&gt;change&lt;/CODE&gt; event instead of &lt;CODE&gt;input&lt;/CODE&gt;? e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;emailfield.dispatchEvent(new Event('change', { 'bubbles': true }));&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It may also be worth trying it with bubbling.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 18:41:31 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762228#M33038</guid>
      <dc:creator>piersg</dc:creator>
      <dc:date>2023-02-28T18:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762582#M33052</link>
      <description>&lt;P&gt;hey &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/103660"&gt;@piersg&lt;/a&gt;, I tried that as well unfortunately it doesn't trigger the validation at all (and after the form submission it again clears the input). It seems that the react form fields only listen to the input event. I also tried it with jquery as it was mentioned in other posts but without success:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;$(companyfield).val("MyCompanyName").change();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2023 12:44:26 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/762582#M33052</guid>
      <dc:creator>paradummy</dc:creator>
      <dc:date>2023-03-01T12:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/763217#M33083</link>
      <description>&lt;P&gt;I haven't been able to get the global event listener to work for changing field inputs for a few months now. Not sure if HubSpot made some change that rendered field changing impossible in the event listener or what, but even some of our old forms that we hadn't touched for over a year just straight up suddenly stopped working. Switched to using the form embed code and putting JavaScript directly in the callback. Bit annoying, but it at least works.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;hbspt.forms.create({
      portalId: '',
      formId: '',
      onFormReady: function($form) {
        // YOUR SCRIPT HERE
      } 
});   &lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2023 13:34:48 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/763217#M33083</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-03-02T13:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/1066250#M41208</link>
      <description>&lt;P&gt;&amp;nbsp;Did anyone find a solution to this? Looking to pass an ID into a hidden field.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2024 12:27:47 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/1066250#M41208</guid>
      <dc:creator>LucyMoss</dc:creator>
      <dc:date>2024-11-07T12:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/1079412#M41493</link>
      <description>&lt;P&gt;Hi! &lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋🏻&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A bit late, but I'm getting this to work in my functions.&lt;/P&gt;&lt;P&gt;Maybe it can help someone trying to figure this out!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;(() =&amp;gt; {
  const contactForms = document.querySelectorAll(".module--custom-form-contact");

  const initContactForm = (contactForm) =&amp;gt; {
    if (contactForm.querySelector(".hs-email input[type=email]")) {
      contactForm.querySelector(".hs-email input[type=email]").value = "myemail@address.com";
      contactForm.querySelector(".hs-email input[type=email]").dispatchEvent(new Event('input', { 'bubbles': true }));
    }
  }

  window.addEventListener('message', event =&amp;gt; {
    if (event.data.type === 'hsFormCallback' &amp;amp;&amp;amp; event.data.eventName === 'onFormReady') {
      contactForms &amp;amp;&amp;amp; contactForms.forEach(contactForm =&amp;gt; {
        if (contactForm.querySelector(".hs-form") &amp;amp;&amp;amp; (event.data.id === contactForm.querySelector(".hs-form").dataset.formId)) { 
          initContactForm(contactForm)
        }
      })
    }
  })
})();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Luc&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 10:00:11 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/1079412#M41493</guid>
      <dc:creator>Luc_Benayoun</dc:creator>
      <dc:date>2024-12-05T10:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: Setting form field values with javascript</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/1177806#M43586</link>
      <description>&lt;P&gt;You can now do this by using the HubSpotFormsV4 JS object. Like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function setHsField(fieldName, value) {
  if (HubSpotFormsV4) {
    var hsForms = HubSpotFormsV4.getForms();
    var spInput = document.querySelector('input[name="0-1/' + fieldName + '"]');

    if (hsForms &amp;amp;&amp;amp; spInput) {
        window.console &amp;amp;&amp;amp; window.console.log('HS Form found. Setting to ' + value);
        hsForms[0].setFieldValue('0-1/' + fieldName, value);     
    }
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Docs are here:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.hubspot.com/docs/guides/api/marketing/forms/global-form-events#setfieldvalue" target="_blank"&gt;https://developers.hubspot.com/docs/guides/api/marketing/forms/global-form-events#setfieldvalue&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Jul 2025 15:55:17 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Setting-form-field-values-with-javascript/m-p/1177806#M43586</guid>
      <dc:creator>OChrzan</dc:creator>
      <dc:date>2025-07-17T15:55:17Z</dc:date>
    </item>
  </channel>
</rss>

