When is your script actually bring loaded? You want to affect the DOM when the page loads, but when that script runs (immediately after the script file has read), has the DOM loaded?
Consider adding a defer attribute to the script tag, or moving it to the very end of the body (once the DOM had loaded)