hiding nested ULs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abs

    hiding nested ULs

    Hi all.

    The example code is here http://instaltech.com.pl/_drzewo/test.html. I'd
    like to create a function which will have <li> object (id="myLI" in example
    code) passed as a parameter and will recursively hide (css display: none)
    all ULs nested below passed LI. No matter if such ULs are nested in LI
    passed as a parameter or in its children or childer of its children etc.
    Could anybody help me ? I've got problems with that recursivelity. Or maybe
    the recursivelity is not he best solution for this ?

    Best regards,
    ABS


  • Random

    #2
    Re: hiding nested ULs

    If your <li> will only have the <span> and other <ul>s as children, you
    might just for(;;) through them and hide all but the first child
    (remember that line breaks are nodes in Mozilla, but not in IE, so
    you'll have a difference in family size)

    No need for recusion if all you want to do is make the children (et al)
    unseen. Leave the grandchildren alone and hide the direct descendants.
    The grandkids will disappear too.

    One loop and you're done.

    If you strictly want to hide only <unordered lists>, you'll need to run
    a loop through all children of your <list item> and hide only those
    children with the nodeName 'UL':

    myElement = document.getEle mentById( 'myLI' );
    for( var c = 0; c < myElement.child Nodes.length; c++ )
    myElement.child Nodes[c].nodeName == 'UL' && (
    myElement.child Nodes[c].style.display = 'none'
    );

    If you want to go through and explicitly hide all <ul>s, regardless of
    how far removed they are from their ancestor, recursion is the way to
    go.

    function hideAllUL( element ) {
    for( var c = 0; c < element.childNo des.length; c++ ) {
    element.childNo des[c].nodeName == 'UL' && (
    element.childNo des[c].style.display = 'none'
    );

    hideAllUL( element.childNo des[ c ] );
    }

    return true;
    }

    Comment

    • ans

      #3
      Re: hiding nested ULs

      Random wrote:[color=blue]
      > If your <li> will only have the <span> and other <ul>s as children,
      > you might just for(;;) through them and hide all but the first child
      > (remember that line breaks are nodes in Mozilla, but not in IE, so
      > you'll have a difference in family size)
      >
      > No need for recusion if all you want to do is make the children (et
      > al) unseen. Leave the grandchildren alone and hide the direct
      > descendants. The grandkids will disappear too.
      >
      > One loop and you're done.[/color]

      I understand but I want the <ul>s which are children to be hidden when I
      will show them back.
      [color=blue]
      > If you want to go through and explicitly hide all <ul>s, regardless of
      > how far removed they are from their ancestor, recursion is the way to
      > go. [...][/color]

      Yes, that's what I needed. Thank you so much for showing me the way :)

      Best regards,
      ABS



      Comment

      • abs

        #4
        Re: hiding nested ULs

        Random wrote:[color=blue]
        > If your <li> will only have the <span> and other <ul>s as children,
        > you might just for(;;) through them and hide all but the first child
        > (remember that line breaks are nodes in Mozilla, but not in IE, so
        > you'll have a difference in family size)
        >
        > No need for recusion if all you want to do is make the children (et
        > al) unseen. Leave the grandchildren alone and hide the direct
        > descendants. The grandkids will disappear too.
        >
        > One loop and you're done.[/color]

        I understand but I want all the <ul>s which are children of <li>s to be
        hidden when I will show them back, no matter how deep they are.
        [color=blue]
        > If you want to go through and explicitly hide all <ul>s, regardless of
        > how far removed they are from their ancestor, recursion is the way to
        > go. [...][/color]

        Yes, that's what I needed. Thank you so much for showing me the way :)

        Best regards,
        ABS









        Comment

        • abs

          #5
          Re: hiding nested ULs

          Random wrote:
          [...]

          Small request: could you tell me what does this construction mean:

          element.childNo des[c].nodeName == 'UL' &&
          (element.childN odes[c].style.display = 'none');
          hideAllUL( element.childNo des[ c ] );

          Is it like if( ... ) than { ... } ? i wonder if I am doing something wrong
          becuse it does not work when I wanted to replace it with if ( ... )

          ABS


          Comment

          • Random

            #6
            Re: hiding nested ULs

            element.childNo des[c].nodeName == 'UL' && (
            element.childNo des[c].style.display = 'none'
            );

            hideAllUL( element.childNo des[ c ] );

            May be rewritten as:

            if( element.childNo des[ c ].nodeName == 'UL' ) {
            element.childNo des[ c ].style.display = 'none';
            }

            hideAllUL( element.childNo des[ c ] );

            Comment

            • RobG

              #7
              Re: hiding nested ULs

              abs wrote:[color=blue]
              > Hi all.
              >
              > The example code is here http://instaltech.com.pl/_drzewo/test.html. I'd
              > like to create a function which will have <li> object (id="myLI" in example
              > code) passed as a parameter and will recursively hide (css display: none)
              > all ULs nested below passed LI. No matter if such ULs are nested in LI
              > passed as a parameter or in its children or childer of its children etc.
              > Could anybody help me ? I've got problems with that recursivelity. Or maybe
              > the recursivelity is not he best solution for this ?[/color]

              Recursion is not required.

              var x = document.getEle mentById('myLI' );
              var uls = x.getElementsBy TagName('UL');
              var i=uls.length;
              while (i--) {
              uls[i].style.display= 'none';
              }

              Presuming you test for getElementById, getElementsByTa gName and style
              object support.

              --
              Rob

              Comment

              • Random

                #8
                Re: hiding nested ULs

                Rob,
                Will that go all the way down the hierarchy to grandchildren,
                great-grandchildren, et cetera? I've never had occasion to use
                getElementsByTa gName()

                Comment

                • RobG

                  #9
                  Re: hiding nested ULs

                  Random wrote:[color=blue]
                  > Rob,
                  > Will that go all the way down the hierarchy to grandchildren,
                  > great-grandchildren, et cetera? I've never had occasion to use
                  > getElementsByTa gName()[/color]

                  Please quote what you are replying to. At a guess:

                  var x = document.getEle mentById('myLI' );
                  var uls = x.getElementsBy TagName('UL');
                  var i=uls.length;
                  while (i--) {
                  uls[i].style.display= 'none';
                  }

                  The simple answer is 'yes'.

                  The syntax is element.getElem entsByTagName(t ag)

                  where 'element' is a reference to an element in the document and 'tag'
                  is an element tag name, e.g. 'P', 'DIV', 'LI', 'SPAN'...

                  It will return a collection of all the elements with the given tag name
                  that are descendants of 'element'. To get a collection of all the
                  elements with the specified tag in the entire document, use:

                  document.getEle mentsByTagName( )

                  To restrict the collection to just the descendants of a particular
                  node, 'element' should be a reference to a specific node.

                  From the Mozilla documentation for getElementsByTa gName:

                  "getElementsByT agName on the element is the same as
                  getElementsByTa gName on the document, except that its search is
                  restricted to those elements which are children of the current
                  element."

                  The term 'children' here means all descendants of the current element,
                  not just the collection returned by 'childNodes'.

                  <URL:http://www.mozilla.org/docs/dom/domref/dom_el_ref42.ht ml#1028771>

                  W3 doco:

                  <URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-A6C9094>



                  --
                  Rob

                  Comment

                  • Random

                    #10
                    Re: hiding nested ULs

                    Rob:
                    Sorry for not quoting. I'm using Google Groups, which I have yet to
                    find a way to configure accordingly. I try to keep my posts' contexts
                    clear, in lieu of trying to manually quote.

                    I'll think of a better solution if it's a problem.

                    Thanks for the info on getElmentsByTag Name() -- interesting stuff.

                    -R

                    Comment

                    • RobG

                      #11
                      Re: hiding nested ULs

                      Random wrote:[color=blue]
                      > Rob:
                      > Sorry for not quoting. I'm using Google Groups, which I have yet to
                      > find a way to configure accordingly. I try to keep my posts' contexts
                      > clear, in lieu of trying to manually quote.
                      >
                      > I'll think of a better solution if it's a problem.
                      >
                      > Thanks for the info on getElmentsByTag Name() -- interesting stuff.
                      >
                      > -R
                      >[/color]

                      To quote replies using Google groups, click the 'Show options' link,
                      then the 'Reply' link that appears just below subject.


                      --
                      Rob

                      Comment

                      • Random

                        #12
                        Re: hiding nested ULs

                        Excellent. Thanks yet again.
                        I'd been using the 'Reply' link below the message, which apparently
                        doesn't want to bother with quoting.

                        Learn something new every day, hey?

                        Thanks again,
                        -R


                        RobG wrote:[color=blue]
                        > Random wrote:[color=green]
                        > > Rob:
                        > > Sorry for not quoting. I'm using Google Groups, which I have yet to
                        > > find a way to configure accordingly. I try to keep my posts'[/color][/color]
                        contexts[color=blue][color=green]
                        > > clear, in lieu of trying to manually quote.
                        > >
                        > > I'll think of a better solution if it's a problem.
                        > >
                        > > Thanks for the info on getElmentsByTag Name() -- interesting stuff.
                        > >
                        > > -R
                        > >[/color]
                        >
                        > To quote replies using Google groups, click the 'Show options'[/color]
                        link,[color=blue]
                        > then the 'Reply' link that appears just below subject.
                        >
                        >
                        > --
                        > Rob[/color]

                        Comment

                        • Mick White

                          #13
                          Re: hiding nested ULs

                          Random wrote:

                          [snip]
                          [color=blue]
                          > I'd been using the 'Reply' link below the message, which apparently
                          > doesn't want to bother with quoting.
                          >
                          > Learn something new every day, hey?
                          >[/color]


                          Apparently not.

                          Mick

                          Comment

                          Working...