Ideally, you would put the JS code into a separate JS file and link it at the bottom of your page just before the closing
The reason being, without going through it line by line, is that the script needs to run after the page had been drawn.
If you link the js file in the
<head> it can’t affect anything as the elements in question do not yet exist on the page.
Anyway, this is clearly a JS issue so I will push this over to that section.