This question already has an answer here:
I noticed the following:
...
Before realizing this, I was always using the following:
var x = document.getElementById('myDiv'); x.style.color = 'red';
I am confused. What's the point of the second approach? Does the first approach always work?
Are IDs for an html element always available from the window object?
No. It is a non-standard Microsoft-ism that some other browsers have adopted for compatibility reasons. It is prone to namespace collisions, and not completely cross-browser compatible: don't do it.
What's the point of the second approach?
It is standard, well-supported cross-browser (and also cross-language).