Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for each element type in body

Tags:

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.


Edit to share that it gives me tremendous joy to look at questions 9-years old and to see how far I've come and that this question still benefits others.

like image 983
jason m Avatar asked Jun 09 '12 19:06

jason m


People also ask

How to get all elements js?

Use the querySelectorAll() method to get all elements by type, e.g. document. querySelectorAll('input[type="text"]') . The method returns a NodeList containing the elements that match the provided selector.

How do I get all the elements in a page?

If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method. This example returns a list of all <p> elements with class="intro" .

How do I get all elements of tag Div inside HTML?

document. getElementsByTagName("div") will return all divs in the document.

What is element and tag in HTML?

HTML tags are used to hold the HTML element. HTML element holds the content. HTML attributes are used to describe the characteristic of an HTML element in detail. HTML tag starts with < and ends with > Whatever written within a HTML tag are HTML elements.


1 Answers

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";
}​
like image 52
gdoron is supporting Monica Avatar answered Oct 06 '22 15:10

gdoron is supporting Monica