Hide all div with javascript

I am wondering how I can hide all divs on the page only using Javascript, I can not use jQuery. Is there a way to do this without using the arrays that come with document.getElementByTag? Or, if not, could you show me how to hide everything? Thank!

+5
source share
3 answers

Use getElementsByTagName()to get a list of all elements div, and then set their CSS displayproperty none.

var divs = ​document.getElementsByTagName("div");​
for (var i = 0; i < divs.length; i++) {
  divs[i].style.display = 'none';        
}

Demo .

+7
source

You will need to use document.getElementsByTagNameand then use the for loop to process all the elements:

var divs = document.getElementsByTagName('div');
for(var i = 0; i < divs.length; i++) {
  divs[i].style.display = "none";
}
+1
source

.

CSS ,

body.hideDivs DIV {

    display: none;

}

document.body.className = "hideDivs";

divs, , .

+1

All Articles