<?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: JS not working on a web page in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747394#M32528</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/91608"&gt;@alyssamwilie&lt;/a&gt;, it's for a HubSpot form&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jan 2023 13:57:05 GMT</pubDate>
    <dc:creator>dzoladz</dc:creator>
    <dc:date>2023-01-26T13:57:05Z</dc:date>
    <item>
      <title>JS not working on a web page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747065#M32521</link>
      <description>&lt;P&gt;Hey All, I have some JS code&amp;nbsp;I added to a JS file and linked it to the base.html. The JS code works correctly in Visual Studio. I know it is linked correctly because when I console.log the variables they show up. The issue is the variables in the console.log show up null or undefined. If the variables are null, obviously the script won't function. I've looked at other posts and followed their recommendations with no luck. Any recommendations would be helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;window.addEventListener('load', function () {&lt;BR /&gt;let questionInputs = document.querySelectorAll('input[type="radio"]');&lt;BR /&gt;let imgCheckImg = document.querySelector('.img_check');&lt;BR /&gt;let currentForm = document.querySelector('form');&lt;BR /&gt;console.log(currentForm);&lt;BR /&gt;console.log(questionInputs);&lt;BR /&gt;console.log(imgCheckImg);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if (questionInputs.length &amp;gt; 0) {&lt;BR /&gt;questionInputs.forEach(questionInput =&amp;gt; {&lt;BR /&gt;// Listen to changes of every inputs once&lt;BR /&gt;questionInput.addEventListener('change', () =&amp;gt; {&lt;BR /&gt;if (questionInput.checked) {&lt;BR /&gt;const imageCheck = questionInput.closest('.hs-form-field').previousElementSibling.querySelector('.img_check')&lt;BR /&gt;if (imageCheck) {&lt;BR /&gt;// Add class to show check mark&lt;BR /&gt;imageCheck.classList.add('img_check--visible');&lt;BR /&gt;imageCheck.classList.remove('img_check');&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}, {once: true});&lt;BR /&gt;});&lt;BR /&gt;}&lt;BR /&gt;});&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jan 2023 22:40:46 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747065#M32521</guid>
      <dc:creator>dzoladz</dc:creator>
      <dc:date>2023-01-25T22:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: JS not working on a web page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747392#M32527</link>
      <description>&lt;P&gt;Is this code for an HTML form or a HubSpot form?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 13:53:42 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747392#M32527</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-01-26T13:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: JS not working on a web page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747394#M32528</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/91608"&gt;@alyssamwilie&lt;/a&gt;, it's for a HubSpot form&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 13:57:05 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747394#M32528</guid>
      <dc:creator>dzoladz</dc:creator>
      <dc:date>2023-01-26T13:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: JS not working on a web page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747400#M32529</link>
      <description>&lt;P&gt;You'll want to use the global form event listener then. HubSpot forms are built with JavaScript so they often aren't completely loaded in when the window itself is considered loaded.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;window.addEventListener('message', event =&amp;gt; {
  // check that form is fully loaded using onFormReady --&amp;gt;
  if(event.data.type === 'hsFormCallback' &amp;amp;&amp;amp; event.data.eventName === 'onFormReady') {
    let questionInputs = document.querySelectorAll('input[type="radio"]');
    let imgCheckImg = document.querySelector('.img_check');
    // select the form by it's id using event.data.id
    let currentForm = document.querySelector('form[data-form-id="'+event.data.id);
    console.log(currentForm);
    console.log(questionInputs);
    console.log(imgCheckImg);


    if (questionInputs.length &amp;gt; 0) {
      questionInputs.forEach(questionInput =&amp;gt; {
        // Listen to changes of every inputs once
        questionInput.addEventListener('change', () =&amp;gt; {
          if (questionInput.checked) {
            const imageCheck = questionInput.closest('.hs-form-field').previousElementSibling.querySelector('.img_check')
            
            if (imageCheck) {
              // Add class to show check mark
              imageCheck.classList.add('img_check--visible');
              imageCheck.classList.remove('img_check');
            }
          }
        }, {once: true});
      });
    }
  }
});&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 26 Jan 2023 14:05:19 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747400#M32529</guid>
      <dc:creator>alyssamwilie</dc:creator>
      <dc:date>2023-01-26T14:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: JS not working on a web page</title>
      <link>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747403#M32530</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/91608"&gt;@alyssamwilie&lt;/a&gt;&amp;nbsp;, that did it! Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 14:13:43 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/JS-not-working-on-a-web-page/m-p/747403#M32530</guid>
      <dc:creator>dzoladz</dc:creator>
      <dc:date>2023-01-26T14:13:43Z</dc:date>
    </item>
  </channel>
</rss>

