Cannot read property 'style' of undefined -- Uncaught Type Error

The Solution to Cannot read property 'style' of undefined -- Uncaught Type Error is


Add your <script> to the bottom of your <body>, or add an event listener for DOMContentLoaded following this StackOverflow question.

If that script executes in the <head> section of the code, document.getElementsByClassName(...) will return an empty array because the DOM is not loaded yet.

You're getting the Type Error because you're referencing search_span[0], but search_span[0] is undefined.

This works when you execute it in Dev Tools because the DOM is already loaded.

~ Answered on 2017-02-21 14:35:58


Most Viewed Questions: