If the <script> tag is before the elements in your document then it won't have access to the elements because they haven't been parsed by the browser yet.
I recommend putting <script> tags at the bottom of the page, right before the closing </body> tag.