hidding div - showing div

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.west.cox.net

    #1

    hidding div - showing div


    I have something like this in a function:

    ....
    var box = document.getEle mentById("box") ;
    box.style.visib ility = 'hidden';
    ....

    This hides a div setup like the following:

    <div id="box">
    <table>
    <tr><td>Content 1</td></tr>
    </table>
    </div>

    But, what if I want to have a another table
    replace the table in the div above.

    Say I want something like this to replace it:

    <table>
    <tr><td>Content 2</td></tr>
    </table>

    For this example I have used simple content
    in my divs. But, in th real case, the div has a
    large group of nested tables of data.

    I want the two to occupy the same space, so
    when one becomes hidden and the other becomes
    visible, it is like they are replacing one another.

    Since I have such large amounts of data in one of the
    tables, I do not want to user innerHTML to do the
    replacement. Is there any other way I can do it?

    Thanks


  • tennisbill

    #2
    Re: hidding div - showing div

    This sounds like something I've done before, where it was accomplished
    by simply having multiple <div>s, all absolutely positioned to the same
    location, and each with its own appropriate text/html fill.
    Then simply make only the current <div> of interest visible, and all
    others that occupy that screen location hidden.
    I tend to write a function that takes as a parameter the id of the
    <div> that should be visible. Then the function runs through a list of
    all the <div>s that occupy that space, making them hidden. Finally, it
    makes visible the one that was referenced in the function call.

    Comment

    • SimonFx

      #3
      Re: hidding div - showing div

      Don't use visibility but display. The following works well in the
      browsers I have tested so far (IE / Netscape / Mozilla / Firebird /
      Safari / Opera). Might also test using "ID=" in the table instead of a DIV


      <HTML>
      <HEAD>
      <script type="text/javascript" language="JavaS cript">
      <!--
      function mySetStyleDispl ay (myDiv, myStyle){
      if (document.all){
      obj = document.all(my Div);
      obj.style.displ ay = myStyle;
      } else if (document.getEl ementById) {
      obj = document.getEle mentById(myDiv) ;
      obj.style.displ ay = myStyle;
      } else {alert (Err_Browser);}
      }

      function myShow(myDiv){
      mySetStyleDispl ay (myDiv,'');
      }

      function myHide(myDiv){
      mySetStyleDispl ay (myDiv,'none');
      }
      -->
      </SCRIPT>

      </HEAD>
      <BODY>
      <A HREF="#"
      onMouseOver="Ja vascript:myShow ('content2');
      myHide('content 1'); return true;"
      onMouseOut="Jav ascript:myShow( 'content1');
      myHide('content 2'); return true;">Test</a>

      <TABLE ID="content1">
      <TR><TD>Content 1</TD></TR></TABLE>

      <TABLE ID="content2" STYLE="display: none;">
      <TR><TD>Content 2</TD></TR></TABLE>

      </BODY>
      </HTML>

      Comment

      • Michael Winter

        #4
        Re: hidding div - showing div

        On Sun, 21 Nov 2004 04:27:25 +0000, SimonFx <neoporcupine@s apm.yahoo.com>
        wrote:
        [color=blue]
        > Don't use visibility but display.[/color]

        Well, it really depends. Sometimes it's desirable not to cause reflow due
        to the appearance of previously hidden content.

        [snip]
        [color=blue]
        > <HTML>[/color]

        Valid documents should have a DOCTYPE definition...
        [color=blue]
        > <HEAD>[/color]

        ....and a TITLE element.
        [color=blue]
        > <script type="text/javascript" language="JavaS cript">[/color]

        Remove the language attribute. It's long deprecated, and type makes it
        redundant.
        [color=blue]
        > <!--[/color]

        That can go, too.
        [color=blue]
        > function mySetStyleDispl ay (myDiv, myStyle){
        > if (document.all){
        > obj = document.all(my Div);
        > obj.style.displ ay = myStyle;
        > } else if (document.getEl ementById) {
        > obj = document.getEle mentById(myDiv) ;
        > obj.style.displ ay = myStyle;[/color]

        It might be better to move the style assignment into common code, and
        check that the style object is supported before using it. Also, the obj
        variable should be made local with the var keyword.

        var obj = null;
        /* Be biased towards the standard method. */
        if(document.get ElementById) {
        obj = document.getEle mentById(myDiv) ;
        } else if(document.all ) {
        obj = document.all[myDiv];
        }
        if(obj && obj.style) {
        obj.style.displ ay = myStyle;
        }

        It would be even better to make document.getEle mentById a reliable
        function. The simplest way to achieve that is:

        if(!document.ge tElementById) {
        document.getEle mentById = document.all ?
        function(i) {return document.all[i];};
        : function() {return null;};
        }

        Better ways can be found in the FAQ notes and the archives of this group.

        Now you can re-write the function knowing that calling
        document.getEle mentById will always be successful:

        var obj = document.getEle mentById(myDiv) ;
        if(obj && obj.style) {
        obj.style.displ ay = myStyle;
        }
        [color=blue]
        > } else {alert (Err_Browser);}[/color]

        There really isn't much point in telling the user about an error. There's
        nothing they can do about it. Just make sure that such an event doesn't
        prevent the user from using the page.

        [snip]
        [color=blue]
        > <A HREF="#"
        > onMouseOver="Ja vascript:myShow ('content2');[/color]

        There is no need to prefix with "javascript :". Most browsers will ignore
        that as a label anyway, and it's only of use in IE if you've used VBScript
        previously.
        [color=blue]
        > myHide('content 1'); return true;"[/color]

        Returning true doesn't do anything. Returning false wouldn't either in
        this case as mouseover isn't a cancellable event. The same comments apply
        to the other link.

        [snip]
        [color=blue]
        > <TABLE ID="content2" STYLE="display: none;">[/color]

        I realise that this is only a demo, but you should warn that hiding the
        table like isn't appropriate on the Web. If content should be hidden
        initially, do it through script otherwise the visitor may never see it.

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Frances Del Rio

          #5
          Re: hidding div - showing div

          I do this on this pg..



          HTH.. Frances


          news.west.cox.n et wrote:
          [color=blue]
          > I have something like this in a function:
          >
          > ...
          > var box = document.getEle mentById("box") ;
          > box.style.visib ility = 'hidden';
          > ...
          >
          > This hides a div setup like the following:
          >
          > <div id="box">
          > <table>
          > <tr><td>Content 1</td></tr>
          > </table>
          > </div>
          >
          > But, what if I want to have a another table
          > replace the table in the div above.
          >
          > Say I want something like this to replace it:
          >
          > <table>
          > <tr><td>Content 2</td></tr>
          > </table>
          >
          > For this example I have used simple content
          > in my divs. But, in th real case, the div has a
          > large group of nested tables of data.
          >
          > I want the two to occupy the same space, so
          > when one becomes hidden and the other becomes
          > visible, it is like they are replacing one another.
          >
          > Since I have such large amounts of data in one of the
          > tables, I do not want to user innerHTML to do the
          > replacement. Is there any other way I can do it?
          >
          > Thanks
          >
          >[/color]

          Comment

          • SimonFx

            #6
            Re: hidding div - showing div

            Thanks Michael, I appreciate the coding pointers very much!

            Comment

            Working...