How to check if the DOM is ready?
There are two points you can have the code executed with a complete DOM tree: "load" and "DOMContentLoaded". The later is fired much earlier than the latter. Document.ready function fires as soon as the DOM is loaded and ready to be manipulated by script. This function is the earliest point in the page load process where script can safely access element in the page's HTML DOM .
Or use the shortcut