Is it really necessary to wait for the \"ready\" (or \"window.onload\") events if your code only manipulates DOM elements that have already been parsed entirely?
The
in your case it is fine because the browser will render your code line by line and in your code id="foo" comes first so it will get that div....but suppose you wrote this script in head tag then the script will run first and it wont get the div with id="foo" because its not yet loaded..its better to write in document.ready method