57

When should you use document.all vs. document.getElementById?

LarsH
  • 27,481
  • 8
  • 94
  • 152
coderex
  • 27,225
  • 45
  • 116
  • 170
  • 14
    `document.all`? Man, *that's* old school! – Pekka Mar 09 '10 at 12:02
  • 5
    Old school? I checked my scripts and they seems to use both options so that older browsers are catered for. So that's not "old school" at all but more like sound programming, showing that the browser makers citing this as a fault are most ignorant and possibly inexperienced coders employed by Google. – WilliamK Jun 30 '13 at 08:23
  • 1
    Fun fact: by unholy convention, `typeof document.all === 'undefined'` – sam Feb 14 '17 at 21:11

8 Answers8

66

document.all is a proprietary Microsoft extension to the W3C standard.

getElementById() is standard - use that.

However, consider if using a js library like jQuery would come in handy. For example, $("#id") is the jQuery equivalent for getElementById(). Plus, you can use more than just CSS3 selectors.

Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63
  • 1
    I vote for keeping `document.all`! Looks like all browsers technically support it in 2023 and what's the harm with keeping it in place? It's so clean: `document.all.fooBar` vs. `document.getElementById('fooBar')` – jordanfb May 11 '23 at 17:17
  • Looks handy indeed. But then, personally, I rarely search for something as simple as an id. And there is no guarantee that (a) browsers will not get rid of such old compatibility code or (b) that they might have already done it. There is no entry for `document.all` on caniuse.com. On mdn, there is a compatibility table which looks good, but they are typically not often updated, once a feature is implemented in a browser. So it's not really usable for checking if there is *still* support. – Phil Rykoff Jun 17 '23 at 20:58
39

document.all is very old, you don't have to use it anymore.

To quote Nicholas Zakas:

For instance, when the DOM was young, not all browsers supported getElementById(), and so there was a lot of code that looked like this:

if(document.getElementById){  //DOM
    element = document.getElementById(id);
} else if (document.all) {  //IE
    element = document.all[id];
} else if (document.layers){  //Netscape < 6
    element = document.layers[id];
}
Kevin Fegan
  • 1,292
  • 1
  • 16
  • 29
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
15

Actually, document.all is only minimally comparable to document.getElementById. You wouldn't use one in place of the other, they don't return the same things.

If you were trying to filter through browser capabilities you could use them as in Marcel Korpel's answer like this:

if(document.getElementById){  //DOM
    element = document.getElementById(id);
} else if (document.all) {    //IE
    element = document.all[id];
} else if (document.layers){  //Netscape < 6
    element = document.layers[id];
}


But, functionally, document.getElementsByTagName('*') is more equivalent to document.all.

For example, if you were actually going to use document.all to examine all the elements on a page, like this:

var j = document.all.length;
for(var i = 0; i < j; i++){
   alert("Page element["+i+"] has tagName:"+document.all(i).tagName);
}

you would use document.getElementsByTagName('*') instead:

var k = document.getElementsByTagName("*");
var j = k.length; 
for (var i = 0; i < j; i++){
    alert("Page element["+i+"] has tagName:"+k[i].tagName); 
}
Community
  • 1
  • 1
Kevin Fegan
  • 1,292
  • 1
  • 16
  • 29
  • 2
    I don't understand how the document.all(i) notation works. Shouldn't it be document.all[i]? – DSoa Oct 08 '14 at 19:46
  • 2
    @DSoa - After some research, I'm not entirely sure which is correct, or if somehow, both would work. [This page](http://www.java2s.com/Tutorial/JavaScript/0280__Document/documentall.htm) uses **`[i]`** like **`document.all[4].name`**, and [this page](http://www.java2s.com/Tutorial/JavaScript/0280__Document/Listallelementsbyreferencethedocumentall.htm) uses **`(i)`** like **`document.all(i).tagName`**. – Kevin Fegan Oct 09 '14 at 02:08
6

document.querySelectorAll (and its document.querySelector() variant that returns the first found element) is much, much more powerful. You can easily:

  • get an entire collection with document.querySelectorAll("*"), effectively emulating non-standard document.all property;
  • use document.querySelector("#your-id"), effectively emulating document.getElementById() function;
  • use document.querySelectorAll(".your-class"), effectively emulating document.getElementsByClassName() function;
  • use document.querySelectorAll("form") instead of document.forms, and document.querySelectorAll("a") instead of document.links;
  • and perform any much more complex DOM querying (using any available CSS selector) that just cannot be covered with other document builtins.

Unified querying API is the way to go. Even if document.all would be in the standard, it's just inconvenient.

Multiversum
  • 323
  • 3
  • 5
  • It took me a while to realise that in IE, `document.all['word']` returned a list of all elements having the id `"word"`. Even if there were multiple elements with the same ID on the page! So replacing this with `document.querySelector('#word')` broke the page. What I had to do was, after making the IDs unique by putting a number behind them, use `document.querySelectorAll('[id^="word"]')`. Hope this will help someone! – Mr Lister Nov 27 '19 at 12:22
6

document.all() is a non-standard way of accessing DOM elements. It's been deprecated from a few browsers. It gives you access to all sub elements on your document.

document.getElementById() is a standard and fully supported. Each element have a unique id on the document.

If you have:

<div id="testing"></div>

Using

document.getElementById("testing"); 

Will have access to that specific div.

Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
2

According to Microsoft's archived Internet Explorer Dev Center, document.all is deprecated in IE 11 and Edge!

Michael Biermann
  • 3,105
  • 27
  • 23
  • Interesting. Now that chrome adopted it, I hoped FF would join, but if even MS drops it, its really gone. I prefered document.all for its brevity: document.all.editor is much nicer to write and read than document.getElementById("editor"). – citykid Sep 03 '13 at 06:46
  • 1
    It's still there in IE11. In fact, it's even there in Edge. But get this: To defeat all those `if (document.all)` checks and such, `Boolean(document.all)` evaluates to `false` (and `typeof document.all` is `"undefined"` and `document.all == undefined` is `true`). But it's there, and it works. This is even [in the JavaScript spec](https://tc39.github.io/ecma262/#sec-IsHTMLDDA-internal-slot). :-) – T.J. Crowder Dec 07 '18 at 17:40
2

Specifically, document.all was introduced for IE4 BEFORE document.getElementById had been introduced.

So, the presence of document.all means that the code is intended to support IE4, or is trying to identify the browser as IE4 (though it could have been Opera), or the person who wrote (or copied and pasted) the code wasn't up on the latest.

In the highly unlikely event that you need to support IE4, then, you do need document.all (or a library that handles these ancient IE specs).

IE5.x does support document.getElementById but does not support document.getElementsByTagName('*'), so you may still need to use document.all

serge
  • 13,940
  • 35
  • 121
  • 205
DOK
  • 32,337
  • 7
  • 60
  • 92
-1

document.all works in Chrome now (not sure when since), but I've been missing it the last 20 years.... Simply a shorter method name than the clunky document.getElementById. Not sure if it works in Firefox, those guys never had any desire to be compatible with the existing web, always creating new standards instead of embracing the existing web.

mike nelson
  • 21,218
  • 14
  • 66
  • 75