# JS not working on a web page

**URL:** <https://community.hubspot.com/t/js-not-working-on-a-web-page/73764>\
**Category:** CMS Development\
**Tags:** design-manager, javascript, html, forms\
**Created:** [January 25, 2023, 10:38pm UTC](https://community.hubspot.com/t/js-not-working-on-a-web-page/73764 "2023-01-25T22:38:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzoladz](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dzoladz/32/62359_2.png) [@dzoladz](https://community.hubspot.com/u/dzoladz)\
**Post date:** [January 25, 2023, 10:38pm UTC](https://community.hubspot.com/t/js-not-working-on-a-web-page/73764/1 "2023-01-25T22:38:42Z")

</div>

Hey All, I have some JS code 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.

window.addEventListener(‘load’, function () {  
let questionInputs = document.querySelectorAll(‘input[type=“radio”]’);  
let imgCheckImg = document.querySelector(‘.img\_check’);  
let currentForm = document.querySelector(‘form’);  
console.log(currentForm);  
console.log(questionInputs);  
console.log(imgCheckImg);

if (questionInputs.length \> 0) {  
questionInputs.forEach(questionInput =\> {  
// Listen to changes of every inputs once  
questionInput.addEventListener(‘change’, () =\> {  
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});  
});  
}  
});

---

<div class="post-metadata">

**Author:** ![alyssamwilie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alyssamwilie/32/2072_2.png) [@alyssamwilie](https://community.hubspot.com/u/alyssamwilie)\
**Post date:** [January 26, 2023, 1:53pm UTC](https://community.hubspot.com/t/js-not-working-on-a-web-page/73764/2 "2023-01-26T13:53:42Z")

</div>

Is this code for an HTML form or a HubSpot form?

---

<div class="post-metadata">

**Author:** ![dzoladz](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dzoladz/32/62359_2.png) [@dzoladz](https://community.hubspot.com/u/dzoladz)\
**Post date:** [January 26, 2023, 1:56pm UTC](https://community.hubspot.com/t/js-not-working-on-a-web-page/73764/3 "2023-01-26T13:56:48Z")

</div>

Hi @alyssamwilie, it’s for a HubSpot form

---

<div class="post-metadata">

**Author:** ![alyssamwilie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alyssamwilie/32/2072_2.png) [@alyssamwilie](https://community.hubspot.com/u/alyssamwilie)\
**Post date:** [January 26, 2023, 2:05pm UTC](https://community.hubspot.com/t/js-not-working-on-a-web-page/73764/4 "2023-01-26T14:05:19Z")

</div>

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.

```lia-code-sample
window.addEventListener('message', event => {
 // check that form is fully loaded using onFormReady -->
 if(event.data.type === 'hsFormCallback' && 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 > 0) {
 questionInputs.forEach(questionInput => {
 // Listen to changes of every inputs once
 questionInput.addEventListener('change', () => {
 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});
 });
 }
 }
});

```

---

<div class="post-metadata">

**Author:** ![dzoladz](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dzoladz/32/62359_2.png) [@dzoladz](https://community.hubspot.com/u/dzoladz)\
**Post date:** [January 26, 2023, 2:13pm UTC](https://community.hubspot.com/t/js-not-working-on-a-web-page/73764/5 "2023-01-26T14:13:43Z")

</div>

@alyssamwilie , that did it! Thank you!
