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. Better option.. (function(window, document, undefined){ // code that should be taken … Read more

How to getElementByClass instead of GetElementById with JavaScript?

The getElementsByClassName method is now natively supported by the most recent versions of Firefox, Safari, Chrome, IE and Opera, you could make a function to check if a native implementation is available, otherwise use the Dustin Diaz method: function getElementsByClassName(node,classname) { if (node.getElementsByClassName) { // use native implementation if available return node.getElementsByClassName(classname); } else { … Read more