2

I just came across this by accident and I want to check if this is actually supposed to happen.

I have a div in my page with id box. In my Javascript I set a style to a variable named box: box.style.webkitTransform = "yadda yadda".

I thought that box was in scope, declared as var box = document.getElementById('box');, but it is not (the declaration is in another function!). Neither is there a var box defined globally or in any other place.

However the style got assigned just fine. So somehow the elements' id can be used globally in Javascript? Convenient, but I'm afraid to use it. I assume if the name is used for an actual variable it will override this behavior.

Steven Lu
  • 41,389
  • 58
  • 210
  • 364

1 Answers1

2

Some browsers add 'named elements' as properties of the document or windowobject. See this SO-question, especially the excellent answers from bobince

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177