jason m jason m - 3 months ago 10
Javascript Question

for each element type in body

For example

For each div in body
div.innerHtml = "poo"
next div


this is obviously psuedo code but demonstrates what i am trying to do.

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