Can I show/hide list items, but have the items start off as hidden?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TAL651
    New Member
    • Sep 2007
    • 20

    Can I show/hide list items, but have the items start off as hidden?

    Hello,

    I am using javascript to show/hide nested lists. I modified some code originally posted by ACoder on an earlier thread. Currently, however, the page loads with everything showing. My goal is the reverse: the page should load with everything hidden, and only by activating the script will more items be revealed. So, the default would look like this:

    Default:

    ITEM 1 Click to show more

    ---

    After click:

    ITEM 1 Click to show less
    Sub Item

    Here is my current javascript code:

    [CODE=javascript]function showhideexample s(anchor) {
    var span = document.getEle mentById('subit em1');
    span.style.disp lay = (span.style.dis play=='block')? 'none':'block';
    anchor.innherHT ML = (anchor.innherH TML=='Show Examples')?'Hid e Examples':'Show Examples';
    }
    [/CODE]

    Thanks!
    Last edited by pbmods; Oct 9 '07, 12:42 AM. Reason: Changed [CODE] to [CODE=javascript].
  • TAL651
    New Member
    • Sep 2007
    • 20

    #2
    I need to make one clarification. I realize that I could just code [HTML]style="display: none"[/HTML] , but that kind of goes against the idea of semantic xhtml.

    Is there a way I can make the nested list start off as hidden, without resorting to entering style markup? Maybe a javascript that activates on page load?

    Thanks.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by TAL651
      I need to make one clarification. I realize that I could just code [HTML]style="display: none"[/HTML] , but that kind of goes against the idea of semantic xhtml.

      Is there a way I can make the nested list start off as hidden, without resorting to entering style markup? Maybe a javascript that activates on page load?

      Thanks.
      You've got it then. Just execute a hide script onload of the page.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by r035198x
        You've got it then. Just execute a hide script onload of the page.
        Or even use CSS instead, e.g. for an element with id "subitem1":
        [CODE=css]#subitem1 { display:none; }[/CODE]

        Comment

        • TAL651
          New Member
          • Sep 2007
          • 20

          #5
          Originally posted by acoder
          Or even use CSS instead, e.g. for an element with id "subitem1":
          [CODE=css]#subitem1 { display:none; }[/CODE]
          Simple and efficient, thanks! Works great.

          Now I have one more issue, this one related to the javascript. I'll show the code first, then ask my question:

          Javascript code:

          Code:
          function expandcollapseBGrows(anchor) {
          	var span = document.getElementById("BGrows");
          	span.style.display = (span.style.display=='block')?'none':'block';
             anchor.innerHTML = (anchor.innerHTML=='hide')?'»':'hide';
             }
          Now, here's my question. This code currently works - when I click on the hide link, my subitems are hidden and I see a right angle quote. When I click on the right angle quote, my subitems appear, and my link changes to "hide."

          If I replace "hide" with "«" in the script, though, I run into a problem. Everything still appears and disappears as it should. However, my link, instead of changing from a right angle quote to a left angle quote, now just changes to a left angle quote, and then stays that way. It doesn't change back to a right angle quote.

          This seems strange, since it switches back and forth without problems if it is text. For some reason, though, entity references seem to throw it off.

          Comment

          • TAL651
            New Member
            • Sep 2007
            • 20

            #6
            Originally posted by TAL651
            Simple and efficient, thanks! Works great.

            Now I have one more issue, this one related to the javascript. I'll show the code first, then ask my question:

            Javascript code:

            Code:
            function expandcollapseBGrows(anchor) {
            	var span = document.getElementById("BGrows");
            	span.style.display = (span.style.display=='block')?'none':'block';
               anchor.innerHTML = (anchor.innerHTML=='hide')?'»':'hide';
               }
            Now, here's my question. This code currently works - when I click on the hide link, my subitems are hidden and I see a right angle quote. When I click on the right angle quote, my subitems appear, and my link changes to "hide."

            If I replace "hide" with "«" in the script, though, I run into a problem. Everything still appears and disappears as it should. However, my link, instead of changing from a right angle quote to a left angle quote, now just changes to a left angle quote, and then stays that way. It doesn't change back to a right angle quote.

            This seems strange, since it switches back and forth without problems if it is text. For some reason, though, entity references seem to throw it off.
            I'm going to move this to a new post, since it's a different question than the original post...

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by TAL651
              Simple and efficient, thanks! Works great.
              JavaScript is great, but not always the best tool for every problem.

              Comment

              Working...