Q:

Toggle display:none to all tags in JavaScript (no jQuery) without classes : is it possible?

I've read similar posts but i only found where it says to add class to the HTML to do what i want, so, basically i have a button, that when i want to click it hides ALL code HTML tags, and when i click it again it shows them. Since i have a lot of code tags in a page it would take me quite a lot to add a class to each one. I've been trying a few things such as 
if(document.getElementsByTagName("CODE").style.display === "block"){
document.getElementsByTagName("CODE").style.display = "none"
}

and something around that, all similar code, but they all either made my browser crash or didn't work. My question is, is it really a MUST to use the class name and check for the class name of it's possible to compare, as i "did" above in the code here, the display content of all tags? ( maybe looping with a for loop each element, i tried that too, with no result. )
i tried every possible thing with my few knowledge so far ( im still studying javascript ). I would really like to know if i am trying to do something really advanced or i just dont see how it can be done. 
Thanks, i hope there wasnt another question like this, i've read all of the ones suggested and none ( except one it said to use class ) was like this.
They all recommend to add classes, so i feel like im trying to do something really impossible. Im not into jQuery yet, so dont talk about it please, thanks. ( first i must learn for good JavaScript )

A:

You have to iterate through the results of document.getElementsByTagName("CODE"), it is an array-like variable. It is a jQuery feature that lets you write .css() to a list of objects and have them all processed. You need something like
ar = document.getElementsByTagName("code");
for (i = 0; i < ar.length; ++i)
   ar[i].style.display = "none";

If you need to toggle code visibility, use this code
ar = document.getElementsByTagName("code"); 
for (i = 0; i < ar.length; ++i)
{ 
   if(ar[i].style.display != "none") //the element is visible
   {
      ar[i].style.display = "none";
   } 
   else
   {
      ar[i].style.display = "block"; //If you need to make it block explicitly, otherwise ""
   } 
} 

Note that the style.display property is initially empty and defaults to inline for code tag but can be set explicitly to other values. Resetting it to '' results in restoring the state.
If you need to change the visibility back and forth without the modification of display mode, you need to save the previous mode (code tags can be displayed not only in block mode). Can be done like this:
ar = document.getElementsByTagName("code"); 
for (i = 0; i < ar.length; ++i)
{ 
   if(ar[i].style.display != "none") //the element is visible, "" or "blocK" or some other value
   {
      ar[i].saved_display = ar[i].style.display; //Save the display mode to a new property of the tag
      ar[i].style.display = "none"; //And hide the element
   } 
   else
   {
      if (typeof ar[i].saved_display === "undefined") //It's the first time we see the element. Display it in default mode
         ar[i].style.display = "";
      else
         ar[i].style.display = ar[i].saved_display; //We know how the element was shown before we hid it, restoring
   } 
} 

