hide all divs without array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dusk
    New Member
    • Jan 2008
    • 7

    hide all divs without array?

    Hi,
    I'm very new to javascript, and I'm having trouble finding a way to display a string of divs without the code getting really messy.

    I have several different pages to write, each with about 15-20 divs, and I need users to be able to select a sequence of divs - basically like a series of yes/no questions that, when "yes" or "no" is selected, displays the appropriate div as well as what's come before it.

    I can hide all divs and display one, no problem. But thats no good cause a user needs to be able see the all the divs which have been selected in sequence.

    I can hide each one individually and show the ones I need individually, but this is going to get messy with long strings of actions for each onclick.

    I understand you can set up an array in the head of the page and use a hideall function like the one below, but the problem here is this: the platform used to view the pages requires a template to be used for each page, and the head section can only be located in the template itself, not the page body. So, I can create a template and store the javascript in the head section, but if I make an array then each page has to have the same number of divs, and they all have to have the same ids.

    So unless I'm missing something, an array in the head section won't work either unless there's a way you can set an array without naming each div in it. (and I don't want to create a new template for each page).

    So the question is, how do I make a simple hideall function given that I can't define an array in the head section?

    (I've also tried putting the array in the body, but the viewing platform doesn't accept this for some reason)

    I've tried a few different scripts, but the one I like best is below, taken from another post in these forums -

    [HTML]<SCRIPT LANGUAGE="JavaS cript">
    //here you place the ids of every element you want.
    var ids=new Array('A','B',' C');

    function switchid1(id){
    hideallids1();
    showdiv1(id);
    }

    function hideallids1(){
    //loop through the array and hide each element by id
    for (var i=0;i<ids.lengt h;i++){
    hidediv1(ids[i]);
    }
    }

    function hidediv1(id) {
    //safe function to hide an element with a specified id
    if (document.getEl ementById) { // DOM3 = IE5, NS6
    document.getEle mentById(id).st yle.display = 'none';
    }
    else {
    if (document.layer s) { // Netscape 4
    document.id.dis play = 'none';
    }
    else { // IE 4
    document.all.id .style.display = 'none';
    }
    }
    }

    function showdiv1(id) {
    //safe function to show an element with a specified id

    if (document.getEl ementById) { // DOM3 = IE5, NS6
    document.getEle mentById(id).st yle.display = 'block';
    }
    else {
    if (document.layer s) { // Netscape 4
    document.id.dis play = 'block';
    }
    else { // IE 4
    document.all.id .style.display = 'block';
    }
    }
    }
    </SCRIPT>

    </head>

    <body>[/HTML]
    Last edited by gits; Jan 22 '08, 10:10 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    you may use the:

    [CODE=javascript]document.getEle mentsByTagName( 'div');[/CODE]
    method for this purpose. that returns a list of all divs in the page. in case you want specified divs to be affected only you may set a class that could act as an secondary identifier:

    [HTML]<div class="secondar y"/>whatever</div>[/HTML]
    so with that we could do:

    [CODE=javascript]var divs = document.getEle mentsByTagName( 'div');

    for (var i = 0, ele; ele = divs[i]; i++) {
    if (ele.className == "secondary" ) {
    // do something with all 'secondary'-class-divs
    }
    }[/CODE]
    kind regards

    Comment

    • dusk
      New Member
      • Jan 2008
      • 7

      #3
      Thanks gits, that seems like its exactly what I'm looking for. But I can't get it to work in my code...

      You can see below there's a button which I made earlier to hide each div separately, and a new button which I tried to make hide all divs with the hideallids function, but its not working.

      If anyone has any ideas they'd be much appreciated!

      (incidentally, I don't think I'll need to use the secondary class element, because at this stage all I'm after is a hideall function that works - I'll use the hideall in conjuction with a few showdiv's to display the ones I need at each point)

      Code:

      [HTML]<head>
      <script language="JavaS cript">

      var divs = document.getEle mentsByTagName( 'div')

      function switchid(id){
      hideallids();
      showdiv(id);
      }

      function hideallids(){
      //loop through the array and hide each element by id
      for (var i=0;i<ids.lengt h;i++){
      hidediv(ids[i]);
      }
      }



      function hidediv(id) {
      //safe function to hide an element with a specified id
      if (document.getEl ementById) { // DOM3 = IE5, NS6
      document.getEle mentById(id).st yle.display = 'none';
      }
      else {
      if (document.layer s) { // Netscape 4
      document.id.dis play = 'none';
      }
      else { // IE 4
      document.all.id .style.display = 'none';
      }
      }
      }

      function showdiv(id) {
      //safe function to show an element with a specified id

      if (document.getEl ementById) { // DOM3 = IE5, NS6
      document.getEle mentById(id).st yle.display = 'block';
      }
      else {
      if (document.layer s) { // Netscape 4
      document.id.dis play = 'block';
      }
      else { // IE 4
      document.all.id .style.display = 'block';
      }
      }
      }

      </script>
      </head>

      <body>

      <a href="javascrip t:showdiv('a1') ;hidediv('b1'); hidediv('b2');h idediv('b3');hi dediv('b4');hid ediv('a2')">Fir st Layer Yes</a>
      <a href="javascrip t:showdiv('a2') ;hidediv('b3'); hidediv('b4');h idediv('b1');hi dediv('b2');hid ediv('a1')">Fir st Layer No</a>
      <input type="button" onclick="javasc ript:hidediv('a 1');hidediv('a2 ')" value="Clear all"</a>
      <input type="button" onclick="javasc ript:hideallids ()" value="Hide all"</a>

      <div id='a1' style="display: none;">
      First Layer Yes Content Yes<BR><BR>

      <input type="button" onclick="javasc ript:showdiv('b 1');hidediv('b2 ')" value="Second Layer Yes"</a>
      <input type="button" onclick="javasc ript:showdiv('b 2');hidediv('b1 ')" value="Second Layer No"</a>

      </div>

      <div id='b1' style="display: none">
      Second Layer Yes Content
      </div>

      <div id='b2' style="display: none">
      Second Layer Content No
      </div>

      <div id='a2' style="display: none;">
      First Layer Content No<BR><BR>

      <input type="button" onclick="javasc ript:showdiv('b 3');hidediv('b4 ')" value="Second Layer Content Yes"</a>
      <input type="button" onclick="javasc ript:showdiv('b 4');hidediv('b3 ')" value="Second Layer Content No"</a>

      </div>

      <div id='b3' style="display: none">
      Second Layer Content Yes
      </div>

      <div id='b4' style="display: none">
      Second Layer Content No
      </div>

      </body>[/HTML]
      Last edited by gits; Jan 22 '08, 10:12 PM. Reason: added code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        in your hideallids-function you refer to ids that should be divs i think :)

        Comment

        • dusk
          New Member
          • Jan 2008
          • 7

          #5
          ok I've had a got with this but still can't get it working. I've tried putting:

          var divs=document.g etElementsByTag Name('div')

          at the start too but no luck.

          Here's the simplified code that 'looks' like it should work to me, but doesn't...

          [html]
          <script language="JavaS cript">

          var ids=document.ge tElementsByTagN ame('div')

          function switchid(id){
          hideallids();
          showdiv(id);
          }

          function hideallids(){
          //loop through the array and hide each element by id
          for (var i=0;i<ids.lengt h;i++){
          hidediv(ids[i]);
          }
          }

          function hidediv(id) {
          //safe function to hide an element with a specified id
          if (document.getEl ementById) { // DOM3 = IE5, NS6
          document.getEle mentById(id).st yle.display = 'none';
          }
          else {
          if (document.layer s) { // Netscape 4
          document.id.dis play = 'none';
          }
          else { // IE 4
          document.all.id .style.display = 'none';
          }
          }
          }

          function showdiv(id) {
          //safe function to show an element with a specified id

          if (document.getEl ementById) { // DOM3 = IE5, NS6
          document.getEle mentById(id).st yle.display = 'block';
          }
          else {
          if (document.layer s) { // Netscape 4
          document.id.dis play = 'block';
          }
          else { // IE 4
          document.all.id .style.display = 'block';
          }
          }
          }

          </script>
          </head>

          <body>

          <a href="javascrip t:showdiv('a1') ;hidediv('a2')" >First Layer Yes</a>
          <a href="javascrip t:showdiv('a2') ;hidediv('a1')" >First Layer No</a>
          <input type="button" onclick="javasc ript:hideallids ()" value="Hide all"</a>

          <div id='a1' style="display: none;">
          First Layer Yes Content<BR><BR>
          </div>

          <div id='a2' style="display: none;">
          First Layer Content No<BR><BR>
          </div>
          </body>
          </html>[/html]

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Put the "var ids = ..." line into the hidealldivs function.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              the problem is that you retrieve the div-node-list during load where the dom is not ready to use ... so to fix that you could use the tip of acoder or retrieve the nodes onload of your document's body ...

              kind regards

              Comment

              • dusk
                New Member
                • Jan 2008
                • 7

                #8
                Thanks for the help, I couldn't find a way to get the var divs = document.getEle mentsByTagName( 'div') line working, but I found another function which works fine, and it uses that line:

                [html]

                <script>

                function clearAllDivs(pa ss) {
                var divs = document.getEle mentsByTagName( 'div');
                for(i=0;i<divs. length;i++){
                if(divs[i].id.match(pass) ){//if they are 'see' divs
                if (document.getEl ementById) // DOM3 = IE5, NS6
                divs[i].style.display= "none";// show/hide
                else
                if (document.layer s) // Netscape 4
                document.layers[divs[i]].display = 'none';
                else // IE 4
                document.all.di vs[i].display = 'none';
                } else {
                if (document.getEl ementById)
                divs[i].style.display= "none";
                else
                if (document.layer s) // Netscape 4
                document.divs[i].display = 'none';
                else // IE 4
                document.all.di vs[i].display = 'none';
                }
                }
                }

                </script>

                </head>

                <body>

                <a href="javascrip t:clearAllDivs( )">Clear all divs</a>

                [/html]

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  glad to hear you got it working :) ... post back to the forum anytime you have more questions ...

                  kind regards

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by dusk
                    Thanks for the help, I couldn't find a way to get the var divs = document.getEle mentsByTagName( 'div') line working, but I found another function which works fine, and it uses that line:
                    If you don't need to support Netscape 4 and IE4, you can trim that function to half its size.

                    Comment

                    • krazin
                      New Member
                      • Jul 2008
                      • 1

                      #11
                      yes, but you didnt make the hidealldivs work.
                      I have that same problem.


                      [CODE=javascript]var ids=new Array();

                      function switchid(id){
                      hideallids();
                      showdiv(id);
                      }

                      function hideallids(){
                      //loop through the array and hide each element by id
                      for (var i=0;i<ids.lengt h;i++){
                      hidediv(ids[i]);
                      }
                      }

                      function hidediv(id) {
                      //safe function to hide an element with a specified id
                      if (document.getEl ementById) { // DOM3 = IE5, NS6
                      document.getEle mentById(id).st yle.display = 'none';
                      }
                      else {
                      if (document.layer s) { // Netscape 4
                      document.id.dis play = 'none';
                      }
                      else { // IE 4
                      document.all.id .style.display = 'none';
                      }
                      }
                      }[/CODE]
                      Last edited by gits; Jul 9 '08, 07:40 AM. Reason: added code tags

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #12
                        please tell exactly what is not working properly, and show where you assign the nodelist ...

                        kind regards

                        Comment

                        Working...