getElementById returns null? [closed]

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.

Use window.onload to wait for the dom nodes to load:

(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);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)