0

I'm trying to add to my frontpage plugin a "Back to top Arrow" like

https://www.w3schools.com/howto/howto_js_scroll_to_top.asp

I have put the JS code to a file and enqueued it. Put the CSS code and enqueued it, an finaly put the HTML code for the button

I was able to enqueue the script file and CSS , since inspecting the page I can see the file and see the code inside. But the button doesn't appear... Inspecting the page I get an error:

script.js?ver=5.3.2:9 Uncaught TypeError: Cannot read property 'style' of null
    at scrollFunction (script.js?ver=5.3.2:9)
    at window.onscroll (script.js?ver=5.3.2:5)

If I put the JS code in the HTML page with the ... It work perfect.

It look like the JS code, when it's in a separate file, can't initialize the variable.

mybutton = document.getElementById("myBtn");

Because it's not ready.

How I can make to be sure that the page is loaded before the call of the document.getElementById?

I'm new to JS so please be simple :)

0 Answers0