# custom javascript not working on website page

**URL:** <https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710>\
**Category:** CMS Development\
**Tags:** ctas\
**Created:** [December 16, 2021, 11:41pm UTC](https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710 "2021-12-16T23:41:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NGraham1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ngraham1/32/44702_2.png) [@NGraham1](https://community.hubspot.com/u/NGraham1)\
**Post date:** [December 16, 2021, 11:41pm UTC](https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710/1 "2021-12-16T23:41:46Z")

</div>

I’m trying to add some animation to my CTAs using vanilla JS:

 ![NGraham1_0-1639697947930.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/b/b13c8ac6ae746ea492e64a4c9dff3b4160c720e6.png)

My issue is that the html collection gotten from the top line is appearing empty when i try to iterate it. I’m doing a lot of console.logs to test and see where the breaking point is but the issue is there. When i put my code in the console on Chrome i have no issue. Any thoughts on what could be wrong? I’m pretty new to JS so I might be making a simple mistake.

---

<div class="post-metadata">

**Author:** ![Oezcan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oezcan/32/53959_2.png) [@Oezcan](https://community.hubspot.com/u/Oezcan)\
**Post date:** [December 17, 2021, 9:12am UTC](https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710/2 "2021-12-17T09:12:32Z")

</div>

Hello @NGraham1 ,

Did you link your script correctly, like at the bottom of the Body HTML element or somewhere else?  
Do you use your script in a module?

Best regards,

Özcan

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [December 17, 2021, 9:54am UTC](https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710/3 "2021-12-17T09:54:00Z")

</div>

Hi @NGraham1 ,  
There may be many reasons to js not working like js not written under \<script\>\</script\> tag if you applying it in template or page level and if you are adding js in the module make sure you add js code in js editor of module  
I see your code if you adding js as I mentioned above then try this in your code  
Add dot(.) with classname  
i.e .learn-more-cta

 ![WbrJsCq](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/f/9fe003cef135f796168560219de5580d64b90c65.png)  
if it does not work try to add animation by getElementById  
Hope this helps!  
If we were able to answer your query, kindly help the community by marking it as a solution.  
Thanks and Regards.

---

<div class="post-metadata">

**Author:** ![albertsg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/albertsg/32/99473_2.png) [@albertsg](https://community.hubspot.com/u/albertsg)\
**Post date:** [December 17, 2021, 2:04pm UTC](https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710/4 "2021-12-17T14:04:38Z")

</div>

Hi @NGraham1, the code seems okay to me, so maybe the issue is not JS related but maybe is related to where you add this code as @Oezcan says.

Please share more information about the location of the code and I’m sure we will find the issue 🙂

---

<div class="post-metadata">

**Author:** ![NGraham1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ngraham1/32/44702_2.png) [@NGraham1](https://community.hubspot.com/u/NGraham1)\
**Post date:** [December 17, 2021, 4:15pm UTC](https://community.hubspot.com/t/custom-javascript-not-working-on-website-page/48710/5 "2021-12-17T16:15:12Z")

</div>

Thanks for the suggestions! It is linked to the right place because my console.logs were showing up in the browser. I did find a solution though.

```lia-code-sample
window.addEventListener('load', function () {
 //I PLACED MY CODE IN HERE AND IT FIXED IT
 });

```

I found this snippit from [this post](https://community.hubspot.com/t5/CMS-Development/How-do-I-target-the-contents-of-a-form-in-HubSpot/td-p/434516) where someone was trying to target elements in a form.

I’m not sure why this is requred but I would also like to know why it’s not common knowledge if it is requried. And why was the answer so hard to find?

Questions for hubspot I suppose. Thanks for the suggestions everyone!
