8

This question might seem silly, but what's the difference between accessing an element (with id "someId") using document.getElementById("someId") Vs. just typing someId ?

eg:

document.getElementById("someId").style.top = "12px";

vs

someId.style.top = "12px";

Here's a sample code http://jsfiddle.net/pRaTA/ (I found that it doesn't work in firefox)

kwa
  • 83
  • 4
  • 2
    The problem with the second form is name clashes. For example, in your jsfiddle change "abc" to "document" and see what happens in each case. – Alohci Dec 01 '10 at 09:52
  • See possible duplicate [are DOM tree elements global variables here?](http://stackoverflow.com/q/3434278/1048572) – Bergi Feb 10 '14 at 19:02

5 Answers5

7

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

Emmett
  • 14,035
  • 12
  • 56
  • 81
  • Yes, I agree. I tested this in firefox and it didn't work. Works well in Chrome/IE though. http://jsfiddle.net/pRaTA/ – kwa Dec 01 '10 at 06:56
2

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>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • Yes, it doesn't work in firefox. btw, it also works in IE! http://jsfiddle.net/pRaTA/ – kwa Dec 01 '10 at 06:58
  • 2
    Exciting. I never knew webkit browsers did that. Here's a potential duplicate http://stackoverflow.com/questions/3434278/ie-chrome-are-dom-tree-elements-global-variables-here – Martin Algesten Dec 01 '10 at 06:59
  • Thanks for that link! Funny, I really thought I'd be laughed at for asking this question! :) – kwa Dec 01 '10 at 07:38
0

In client side scripting, we do document.getElementById to get the element obj in the web page. To retrieve the particular element from the DOM and its properties & methods, you need to use the getElementById method. In Jquery you can simply do var objX = $('#id')

Where as writing down id straight will not do the job. Because how will you extract the element obj from the DOM without traversing the document. document.getElementById method will pull the element information from the document. I hope this make some sense.

Karan
  • 3,265
  • 9
  • 54
  • 82
0

Using just the someId is an old way of selecting an element (i think this was implemented by IE). document.getElementById is the standard one which works on all browsers.

Considering the scenario that you only support the old IE browsers. document.getElementById is still more reliable and readable. Reliable if you are using all numbers for ids.

for example:

input element id="123"

document.getElementById('123').value; // this one works
123.value; // this one doesn't
gianebao
  • 17,718
  • 3
  • 31
  • 40
  • read the 1st sentence of 2nd paragraph, "Considering the scenario that you only support the old IE browsers." – gianebao Dec 01 '10 at 07:46
0

Accessing elements as properties of the global object named after the ID of the element (your second example) is a convenience that originated in IE and has been copied by Safari and Chrome. It's a really bad idea and you shouldn't use it. The main issue is naming collisions (for example, elements with perfectly valid ids such as "alert" or "document").

document.getElementById(), on the other hand, is long-established DOM standard and, apart from some idiotic IE flaws, is foolproof. Always use it in preference to the other form.

Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536