all. Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.
getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
The difference is that GetElementByID() will retrieve a single element object based on the unique id specified, whereas GetElementsByTagName() will retrieve an array of all element objects with the specified tag name. Then you can obtain the value using GetElementById from your C++ code.
Using getElementById is the only good way to access the element, which can be done either by itself or preferably, with a function so that we can more appropriately handle errors if it can't be found. For allowing access to elements blocked by global id, this one goes to getElementById.
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.
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];
}
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With