Hide all classes called container, but the first one?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • municipal
    New Member
    • Jan 2007
    • 4

    Hide all classes called container, but the first one?

    Hi,

    I have this following code in my html page

    Code:
    <div class="container">
    	<div ...
    </div>
    <div class="container">
    	<div ...
    </div>
    ... and so on
    I want to be able to, through some Javascript, hide all div with the class "container" but the first one...

    is it possible?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Do you specifically need that for any number of divs, the first one remains visible while the rest are hidden? If not, set an id for each div like so
    Code:
    <div id="idname" ...>
    then use the following to hide a div
    Code:
    document.getElementById("idname").style.visibility = 'hidden';

    Comment

    • Romulo NF
      New Member
      • Nov 2006
      • 54

      #3
      well.. i hope thats what you looking for:

      Code:
      var divs = document.getElementsByTagName("DIV");
      
      for (x=1; x<divs.length; x++) {
      if (divs[x].className=="container) {
      divs[x].style.display = "none";
      }
      }
      the loop is starting with 1 because 0 is your first div

      important: that code is assuming ALL divs in your page are containers
      for example if you have 5 divs.. and only 3 are containers... and the other 2 are before em in the html structure, the code will hide all the containers!

      Comment

      • Romulo NF
        New Member
        • Nov 2006
        • 54

        #4
        Code:
        <script>
        
        var divs = document.getElementsByTagName("DIV");
        var number = 1
        
        	for (x=0; x<divs.length; x++) {
        		if (divs[x].className=="container") {
        		divs[x].order=number
        			if (number>1) {
        			divs[x].style.display="none";
        			}		
        		number = number+1
        		}
        	}
        
        </script>
        use something like that so you dont have to worry if you have more divs with others or without classes in the same page!

        Comment

        • municipal
          New Member
          • Jan 2007
          • 4

          #5
          thanks for the reply guys, I really appreciate it.

          Romulo NF: I was almost there, but didn't know I could do this:

          Code:
          divs[x].className=="container"
          now I know and it should all work out

          thanks again! :)

          Comment

          Working...