Moving a – MickG Dec 22 '17 at 19:34

  • Your script needs HTML elements fully rendered (DOM built). See https://stackoverflow.com/a/8716680/2055998 – PM 77-1 Dec 22 '17 at 19:34
  • 2
    JS scripts element contents are executed synchronously inline (unless configured otherwise). Any JS expression evaluated prior to "document ready" can only refer to elements *already* fully parsed (eg. opened *and* closed) in the *preceding HTML* markup being loaded. Moving the script location to the top has the `document.getElementById("defaultOpen")` being executed *before* the element is available to manipulate. This error does not occur when the script is at the bottom because the element has been created. – user2864740 Dec 22 '17 at 19:40
  • 1 Answers1

    0

    That's happens because the script run before document is ready. ( sync ). If you want to keep your script at the beginning of the body, or in head, use de window.onload function.

    window.onload = function()
    { 
     // your code goes here
    }
    

    My suggestion will be to place the script at the end of the body, also the bootstrap and jquery libs, because those scripts will be fired after the DOM is ready.

    Andrei Todorut
    • 4,260
    • 2
    • 17
    • 28