When should you use document.all
vs. document.getElementById
What is your answer?
When should you use document.all
vs. document.getElementById
What is your answer?
document.all
is a proprietary Microsoft extension to the W3C-standard. getElementById
is standard - use that.
Evaluate if using a js library like jquery would come in handy :)
$("#id")
is the jquery equivalent for getElementById. PLUS, you can use more than even only css3 selectors.
edit: w3c
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];
}
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.