I was getting a problem .
It's simple, JavaScript will go from up-down, unless you tell it to do something else. By the time it reaches the li elements, the JavaScript code has already been completed.
If you want to keep it in the head however, you could use the document.ready function and it will load only after the HTML it's loaded.