for each element type in body

NodeList.forEach Chrome  iconFirefox iconEdge    iconOpera   iconSafari  icon10

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 Chrome  iconFirefox iconEdge    iconOpera   iconSafari  icon9

const elements = document.querySelectorAll('div');

Array.from(elements).forEach( (el) => {
  el.innerHTML = "foo";
});

Array.from()MDN


Array Destructuring Chrome  iconFirefox iconEdge    iconOpera   iconSafari  icon9

const elements = document.querySelectorAll('div');

[...elements].forEach( el => {
  el.innerHTML = "foo";
});

Destructuring assignmentMDN
Document.querySelectorAll()MDN


Array forEach.call Chrome  iconFirefox iconEdge    iconIExplor icon9Opera   iconSafari  icon

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";
}​

Tags:

Javascript

Dom