I have the following code:
var images = document.getElementsByTagName('img');
if (screen.width > 640) {
for (var image in images) {
image = images[image];
console.log(image['src']);
}
}
There is only one image on the page, but for some reason the for ... in statement is looping through images three times. Why is this?
You’re not just looping through the list of elements, but also the named methods:
lengthitemnamedItemThe correct approach is:
Instead of using
document.getElementsByTagName('img'), you can also usedocument.images.http://jsfiddle.net/C8egs/2/