0

Why am I getting a console error "Uncaught TypeError: Cannot set property 'ondragover' of null"

<html>
  <head>
    <title></title>
    <script>

    var dragzone = document.getElementById('DragZone');

    dragzone.ondragover=function() {
        console.log("Bang");
        return false;
    };

    </script>

 </head>

<body>
    <div id="DragZone">Drag a file over here</div>
</body>
</html>
Bill
  • 2,602
  • 2
  • 35
  • 68

1 Answers1

2

You are trying to access the DOM element before it was created. Move the script to the bottom of <body> and it will work.

<html>
<head>
    <title></title>
</head>
<body>
    <div id="DragZone">Drag a file over here</div>
    <script>
        var dragzone = document.getElementById('DragZone');
        dragzone.ondragover=function() {
            console.log("Bang");
            return false;
        };
    </script>
</body>
</html>
Griffith
  • 3,061
  • 1
  • 13
  • 30