document.getElementbyId returns null

I had a strange issue today. document.getElementbyId returns null, but the object was present in the view source . then I figured out that the javascript was executed on page load ,before the object was rendered. Solution: I set the defer property for the script tag, it worked <script type=”text/javascript” language=”javascript” defer >  

3