Because your script
tag is above the HTML defining the elements that it acts on, it can’t find them because they don’t exist as of when that code runs. Here’s the order in which things are happening in your page:
- The
html
andhead
elements are created - The
meta
element is created and its content noted by the parser - The
script
element for jQuery is created - The parser stops and waits for the jQuery file to load
- Once loaded, the jQuery file is executed
- Parsing continues
- The
script
element for your code is created - The parser stops and waits for your file to load
- Once loaded, your script code is run — and doesn’t find any elements, because there are no
div
elements yet - Parsing continues
- The browser finishes parsing and building the page, which includes creating the elements you’re trying to access in your script
Ways to correct it:
-
Move the
script
elements to the very end, just before the closing</body>
tag, so all of the elements exist before your code runs. Barring a good reason not to do this, this is usually the best solution. -
Use jQuery’s
ready
feature. -
Use the
defer
attribute on the script element, but beware that not all browsers support it yet.
But again, if you control where the script elements go, #1 is usually your best bet.