for each element type in body
NodeList.forEach 10
querySelectorAll
returns a static non-live NodeList
exposing a forEach
method:
const elements = document.querySelectorAll('div');
elements.forEach( el => {
el.innerHTML = "foo";
});
Document.querySelectorAllMDN
NodeList.prototype.forEach()MDN
Array.from 9
const elements = document.querySelectorAll('div');
Array.from(elements).forEach( (el) => {
el.innerHTML = "foo";
});
Array.from()MDN
Array Destructuring 9
const elements = document.querySelectorAll('div');
[...elements].forEach( el => {
el.innerHTML = "foo";
});
Destructuring assignmentMDN
Document.querySelectorAll()MDN
Array forEach.call 9
var elements = document.querySelectorAll('div');
[].forEach.call(elements, function( el ) {
el.innerHTML = "foo";
});
Array.prototype.forEach()MDN
For loop
var elements = document.getElementsByTagName('div');
for (var i=0; i<elements.length; i++) {
elements[i].innerHTML = "foo";
}
Element.getElementsByTagName()MDN
var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "foo";
}
Live DEMO
If you want to look only in the <body>
:
var elements = document.body.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "foo";
}