It's my understanding that every modern browser, both desktop and mobile, surfaces elements with IDs to the global JavaScript namespace. Still, people all over the industry keep using document.getElementById()
or its jQuery cousin $("#TheID")
.
For cases that are not pathological (e. g. ID that matches another object in the global namespace), is there any reason for doing so, other than misplaced concern for backwards compatibility? How far back in time do you have to go so that it starts mattering?
EDIT: consider the following:
<html>
<body>
<input type="hidden" id="Foo" value="Hello world"/>
<input type="button" onclick="alert(Foo.value);" value="Press this"/>
</body>
</html>
It displays "Hello world" on MSIE on Windows, Chrome, Firefox, Opera, Safari on Mac, Safari on iPad, Android browser, MSIE on Windows Phone... Why would anybody use document.getElementById("Foo").value
instead?