1

Possible Duplicate:
IE/Chrome: are DOM tree elements global variables here?
Element accessible with ID

I didn't realise until recently that you can access a DOM element by it's ID without using document.getElementByID()

​<div id="test">
  hi there
</div>​​​​​​​​​​​​​​​​​​​​​​​​​

​alert(test.textContent);​ //alerts "hi there"

What's weirder is that it is supported in FF, Chrome, Safari and IE9+

The only problem I can see is if you overwrite the name ie) var test=3; and it would no longer work. But you have that same problem when setting var test = document.getElementById('test');

My questions are:

-Why/how does this work... Is the browser accessing it through the window object?

-Is it a bad practice?

-Is it supported by older browsers (IE6)?

Community
  • 1
  • 1
gkiely
  • 2,987
  • 1
  • 23
  • 37

0 Answers0