0

So if i have element with id asd. And i go write asd in console it will get selected. And if i write asd.addEvent...etc in script file it will work. I didn't use getElementById(.. or anything. I am confused since when did it start happening. And is it only chrome specific thing. Or Firefox and other browsers have it too. Then Why even type documentGetElementById()... I tried incognito mode and it worked there too. So it's not some plugins messing up.

Summary: I type id and element is selected.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • 1
    possible duplicate of [Do DOM tree elements with ids become global variables?](http://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables) – Jose Ch. Nov 22 '14 at 18:07
  • i think yea thats it, good to know i wasn't going crazy.. :) – Muhammad Umer Nov 22 '14 at 18:34

1 Answers1

0

As pointed out this appears to be a duplicate of Do DOM tree elements with ids become global variables?

Stick to document.getElementById, which is more widely-supported and less ambiguous. You can write a trivial wrapper function with a shorter name if you don't like the typing.

The short and simple answer is that compatibility is king: When you publish code for the public web with its hundreds of browser variants, taking shortcuts which potentially exclude some of your audience is a bad idea.

Community
  • 1
  • 1
Tom Rees
  • 681
  • 4
  • 17