for loop html elements javascript code example
Example 1: loop through html nodelist
const fakeImages = document.querySelectorAll(".fake-image");
for (var i = 0; i < fakeImages.length; i++) {
console.log('fakeImage: ', fakeImages[i]);
}
for (const fakeImage of fakeImages) {
console.log('fakeImage: ', fakeImage);
}
for (const fakeImage of fakeImages.entries()) {
console.log('fakeImage: ', fakeImage);
};
for (const fakeImage of fakeImages.values()) {
console.log('fakeImage: ', fakeImage);
};
for (const fakeImage of fakeImages.keys()) {
console.log('fakeImage: ', fakeImage);
};
fakeImages.forEach(fakeImage => {
console.log('fakeImage: ', fakeImage);
});
[...fakeImages].forEach(fakeImage => {
console.dir(fakeImage);
});
Example 2: loop through dom elements javascript
var all = document.getElementsByTagName("*");
for (var i=0, max=all.length; i < max; i++) {
}
Example 3: how to loop over dom objects javascript
Array.from($('.'+$( this ).attr('id'))).forEach(function(obj){
console.log(obj);
});