document.getElementById("someId") Vs. someId

The difference is that while someId works in some browsers, document.getElementById("someId") actually complies with the W3C standard.


Declaring a element DOM id doesn't mean it's available as a global variable in all browsers. The only cross compatible way to get that is to first do.

var someId = document.getElementById("someId");

Edit: I made this test code which verifies that webkit based browsers seem to make the id available as a var without first declaring it. According to this, also IE will show this behaviour.

  • Firefox: object/undefined
  • Safari: object/object
  • Chrome: object/object
  • IE: object/object (unverified)

Code:

 <html>
    <head>
    </head>
 <body>
   <div id="foo"></div>
   <script type="text/javascript">

     alert("getElementById: "+typeof document.getElementById("foo"));
     alert("as a var: "+typeof foo);

   </script>
 </body>