HTML :
<body>
<canvas id="canvas1"></canvas>
<canvas id="canvas2"></canvas>
</body>
Javascript :
window.onload = function() {
var canvasElements = document.getElementsByTagName("canvas");
for ( var index in canvasElements) {
}
for ( var index = 0; index < canvasElements.length; index++) {
}
}
These two loop does not have same iteration number. Do you know why ?
document.getElementsByTagName("canvas")returns an HTMLCollection that have one propertie, “length” and two methods, “item” and “namedItem”.So the FOR … IN loop iterate over items in that collection and over the members of the HTMLCollection.
So There is 5 iterations:
The for(…;…;…) loop only iterate over items because document.getElementsByTagName(“canvas”)[index] refere to an item
Hope this response was clear !
Ask for more information otherwise…
Some references :