This can happen if the JavaScript code is executed before the page is fully loaded. Try moving the <script> in the HTML document to the end. For example: <html> <body> // HTML Code <script src="filename.js"></script> </body> </html>