document.getElementById('id of div that definately exists')
returns null.
I originally loaded the javascript last in order to make sure I wouldn't need to worry about the onload event. I also tried using the onload event. It's very spooky. Any ideas or help would be greatly appreciated.
This error TypeError: document. getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.
To solve the "getElementById is not a function" error, make sure to spell the getElementById() method correctly, as it is case-sensitive, and only call the method on the document object, e.g. document. getElementById('btn') . Copied!
The getElementById() method returns null if the element does not exist.
Also be careful how you execute the js on the page. For example if you do something like this:
(function(window, document, undefined){ var foo = document.getElementById("foo"); console.log(foo); })(window, document, undefined);
This will return null because you'd be calling the document before it was loaded.
Better option..
(function(window, document, undefined){ // code that should be taken care of right away window.onload = init; function init(){ // the code to be called when the dom has loaded // #document has its nodes } })(window, document, undefined);
It can be caused by:
Please, post your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With