document.queryselectorall foreach code example
Example 1: js foreach querySelectorAll
const allSpanElements = document.querySelectorAll('span');
allSpanElements.forEach((spanElement) => {
spanElement.innerHTML = "This Content will appear on every span Element now";
});
Example 2: 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 3: document queryselectorall and map javacript
[...document.querySelectorAll('div')].map(x => console.log(x.innerHTML))