I'm learning JS from "Professional Javascript for Web Developers" and I can't play with DOM tree because it doesn't seem to work.
Here's an example I'm testing locally:
It alerts "DIV" on jsfiddle, while locally I'm getting this in the console:
Uncaught TypeError: Cannot read property 'tagName' of null
Here's my index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS sandbox</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="myDiv"></div>
</body>
</html>
And script.js
var div = document.getElementById("myDiv");
alert(div.tagName);
What am I missing? I've been trying other stuff like cloning etc. but getElementById() also returns null every time...