<?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: How do I target the contents of a form in HubSpot? in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438626#M23155</link>
    <description>&lt;P&gt;Incredibly helpful - thank you.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 May 2021 13:48:26 GMT</pubDate>
    <dc:creator>nikodev</dc:creator>
    <dc:date>2021-05-18T13:48:26Z</dc:date>
    <item>
      <title>How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/434516#M22886</link>
      <description>&lt;P&gt;Hello there!&lt;BR /&gt;&lt;BR /&gt;I'm trying to insert values into hidden fields on a HubSpot form. I can't for the life of me figure out what's going on here.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;This should be very simple. However, when testing in console, trying to target these elements are yielding null or undefined. Does anyone know how to get around this?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I'm simply trying to find text on the page and insert it into hidden form fields before the form submission goes through - I'm baffled by the issue I'm having.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here is my code:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;script src="" defer&amp;gt;	
document.addEventListener("DOMContentLoaded", function() {	
 let formField1 = document.querySelector("input[name='test1']")
 let valueToInsert = document.getElementById("first").innerText
 formField1.value = valueToInsert
});
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;BR /&gt;I'd really appreciate any help you could provide!&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 18:45:50 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/434516#M22886</guid>
      <dc:creator>nikodev</dc:creator>
      <dc:date>2021-05-05T18:45:50Z</dc:date>
    </item>
    <item>
      <title>Re: How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/434545#M22887</link>
      <description>&lt;P&gt;In case anyone ever has the misfortune of running into the same issue I did, picking up form elements held within a module seems to require:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;script&amp;gt;	
    window.addEventListener('load', function () {
        let valueToInsert = document.getElementById("first").innerText
        let formField = document.querySelector("input[name='test1']")
				formField.value = valueToInsert
        console.log(formField.value)
    });
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 05 May 2021 19:19:32 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/434545#M22887</guid>
      <dc:creator>nikodev</dc:creator>
      <dc:date>2021-05-05T19:19:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/434736#M22897</link>
      <description>&lt;P&gt;The DOMContentLoaded event happens before Hubspot forms are loaded, so your variable targeting the input returns undefined because the input doesn't exist yet.&amp;nbsp;&lt;SPAN&gt;It happens before stylesheets, images, and subframes etc have loaded. You could change it to the &lt;A href="https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event" target="_blank" rel="noopener"&gt;load&lt;/A&gt; event (fired after all resources have loaded) or, better yet, use the onFormReady event from the&amp;nbsp;&lt;A href="https://legacydocs.hubspot.com/global-form-events" target="_blank" rel="noopener"&gt;Hubspot global form events&lt;/A&gt; to make sure the form has loaded and the fields exist:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;window.addEventListener('message', event =&amp;gt; {
  if(event.data.type === 'hsFormCallback' &amp;amp;&amp;amp; event.data.eventName === 'onFormReady') {
    let formField1 = document.querySelector("input[name='test1']")
    let valueToInsert = document.getElementById("first").innerText
    formField1.value = valueToInsert
  }
});&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 May 2021 08:26:00 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/434736#M22897</guid>
      <dc:creator>piersg</dc:creator>
      <dc:date>2021-05-06T08:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438340#M23141</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/103660"&gt;@piersg&lt;/a&gt;&amp;nbsp;Thanks for getting back to me! Totally unfamiliar with the 'message' event. Very useful. I thought my script started working, but I'm still having issues. To add a bit more inconvenience, this script is being loaded within a Shopify page I don't have access to, so my ability to test through trial and error is rather limited.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;My latest iteration is:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;script&amp;gt;    
    window.addEventListener('load', function () {
        let itemNumber = document.getElementsByClassName("text-slate")[0].children[0].innerText
        let price = document.getElementsByClassName("actual-price")[0].innerText
        let itemFormField = document.querySelector("input[name='customerinterests']")
        let priceFormField = document.querySelector("input[name='asking_price']")
        itemFormField.value = itemNumber
        priceFormField.value = price
    });
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;The page it is being utilized on is a Shopify page -&amp;nbsp;&lt;A href="https://rauantiques.com/collections/antiques/products/silver-gilt-exhibition-tea-table-by-maison-aucoc?variant=34759888961671" target="_blank"&gt;https://rauantiques.com/collections/antiques/products/silver-gilt-exhibition-tea-table-by-maison-aucoc?variant=34759888961671&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;I'm not sure if it matters, but the form is held within a modal through the "request more information" button found on the above page. While trying to test what I could with the developer tools, I noticed that when I actually had the form up on the page, and physically identified the appropriate form inputs, only then were my querySelectors working, and not at any point before, which I find strange. While the form was up, I'm also losing the ability to identify the elements that I want to populate the input values with.&amp;nbsp;I'm not sure if this is due to some quirk within the developer tools (the javscript context would switch from "top" to the actual form iframe). I don't know if my script would behave the same way in the real application.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Any help you could provide would be really appreciated.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Is using the onFormReady more appropriate? I'd have to use JS - my jQuery knowledge is nonexistent. If so, would that look like this?:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;hbspt.forms.create({
      portalId: '',
      formId: '',
      onFormSubmit: function($form) {
                let itemNumber = document.getElementsByClassName("text-slate")[0].children[0].innerText
        let price = document.getElementsByClassName("actual-price")[0].innerText
        let itemFormField = document.querySelector("input[name='customerinterests']")
        let priceFormField = document.querySelector("input[name='asking_price']")
	itemFormField.value = itemNumber
        priceFormField.value = price
        }      &lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 May 2021 18:44:23 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438340#M23141</guid>
      <dc:creator>nikodev</dc:creator>
      <dc:date>2021-05-17T18:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438354#M23142</link>
      <description>&lt;P&gt;I suspect a big part of my issue is that the form is being loaded within an iframe.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 May 2021 19:41:00 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438354#M23142</guid>
      <dc:creator>nikodev</dc:creator>
      <dc:date>2021-05-17T19:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438514#M23147</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/152964"&gt;@nikodev&lt;/a&gt;&amp;nbsp;Yes, you&amp;nbsp;can't&amp;nbsp;access an iframe&amp;nbsp;with a different origin using JavaScript, it would be a huge security flaw if you could. The iframe's source is "&lt;A href="https://go.rauantiques.com/.." target="_blank" rel="noopener"&gt;https://go.rauantiques.com/..&lt;/A&gt;." which is a different hostname from the site so counts as a different origin. If you had access to the page/domain you could create a work around using &lt;A href="https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage" target="_blank" rel="noopener"&gt;postMessage.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 09:46:12 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438514#M23147</guid>
      <dc:creator>piersg</dc:creator>
      <dc:date>2021-05-18T09:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I target the contents of a form in HubSpot?</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438626#M23155</link>
      <description>&lt;P&gt;Incredibly helpful - thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 13:48:26 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/m-p/438626#M23155</guid>
      <dc:creator>nikodev</dc:creator>
      <dc:date>2021-05-18T13:48:26Z</dc:date>
    </item>
  </channel>
</rss>

