Simple JS doesnt work (offsetTop)

I have a simple JS code, it has to assign a class to a container when it is close to the top border of the screen. I remember that I implemented something like this in the past and it worked, but now it doesn't (nothing happens). What am I doing wrong?   window.onscroll = function() {myFunction()}; var testarray = document.getElementsByClassName(".testme"); var test= testarray[0]; console.log(test); var active = test.offsetTop; function myFunction() { if (window.pageYOffset >= active + 0) { test("test-active"); } else { test.classList.remove("test-active"); } }   I tried to debug it but I don't see anything in the console, is there any other way or am I doing something wrong?     Then I tried to test in manually in the console but it seems it can't find the container with the class .test-container   Even though it is literally next to it on the page!  
1 answers

Firstly, it looks like you don't test to see if testarray has any results. If it has no results, you won't be able to access the offsetTop property of the first result. I would add a test to see if you have results before trying to use them.


Secondly, it looks like you are trying to manipulate an element called chapter, but you don't define this anywhere, so the onscroll event handler won't work.


I hope this helps.