Problem with hiding/unhiding DIV

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

    #1

    Problem with hiding/unhiding DIV

    Hi,

    I'm trying to figure out how to hide text in a DIV block and then hide
    and unhide it using javascript. In the code that follows, the message
    is initially invisible, and when you click on the "show" link, it's
    made visible and pops up bettween the two hyphen lines. When you
    click on the "hide" link, it's made invisible again. Or at least
    that's what is supposed to happen.

    It does all of this just fine in IE5, IE5.5, and IE6. However, in
    Mozilla 1.5, the display attribute doesn't get changed, and the
    message text remains hidden.

    A google search of past postings mentions that this doesn't work in
    Netscape 4, but it *is* supposed to work in Netscape 6, which I guess
    is approximately equivalent to Mozilla 1.0.

    Is there something different I need to do to get this to work in the
    Netscape/Mozilla family of browsers?

    Thanks.
    MCheu

    ----------------------------------------------------------------------------------------------


    <HTML>
    <!---- Test.html --->
    <HEAD>
    <SCRIPT LANGUAGE="JavaS cript">
    <!--

    // Makes an element visible

    function showElement(ele ment)
    {
    //element.style.d isplay='';
    element.style.d isplay='block';
    }

    // Makes an element disappear

    function hideElement (element)
    {
    element.style.d isplay='none';
    }

    -->

    </SCRIPT>
    </HEAD>

    <BODY>

    <A HREF="#nowhere" onclick="showEl ement(id1);">Sh ow</A>

    <BR>

    <A HREF="#nowhere" onclick="hideEl ement(id1);">Hi de</A>

    <BR><BR>


    --------------------------------------<BR>
    <DIV ID="id1" style="display: none">
    CAN YOU SEE ME?
    </DIV>
    --------------------------------------<BR>
    </HTML>


    ---------------------------------------------------------------------------------------------

    ----------------------------------------
    Thanks,
    MCheu
  • Michael Winter

    #2
    Re: Problem with hiding/unhiding DIV

    On Sat, 06 Mar 2004 01:10:03 -0500, mcheu <mpcheu@yahoo.c om> wrote:
    [color=blue]
    > It does all of this just fine in IE5, IE5.5, and IE6. However, in
    > Mozilla 1.5, the display attribute doesn't get changed, and the
    > message text remains hidden.[/color]

    [snip]
    [color=blue]
    > Is there something different I need to do to get this to work in the
    > Netscape/Mozilla family of browsers?[/color]

    The problem is how you call the function:

    <A HREF="#nowhere" onclick="showEl ement(id1);">Sh ow</A>

    Here, you treat id1, the id of the DIV element, as a global variable. IE,
    and some other browsers, honour this, but there is no reason why they
    should. That's why stricter Gecko-based browsers fail. Instead, use a
    function like:

    function getById( id ) {
    if( document.getEle mentById ) {
    return document.getEle mentById( id );
    } else if( document.all ) {
    return document.all[ id ];
    }
    return null;
    }

    and call the hide/show functions like:

    <A HREF="#nowhere" onclick="showEl ement(getById(' id1'));">Show</A>

    This should now work on browsers that support the style property and the
    document.getEle mentById() method or .all[] collection. That should account
    for the vast majority of modern browsers.

    [snip]
    [color=blue]
    > <SCRIPT LANGUAGE="JavaS cript">[/color]

    The language attribute is deprecated. Use the type attribute instead (it's
    required anyway):

    <script type="text/javascript">
    [color=blue]
    > <!--[/color]

    The practice of hiding scripts is now obsolete. Remove the SGML comment
    delimiters.
    [color=blue]
    > function showElement(ele ment)
    > {
    > //element.style.d isplay='';
    > element.style.d isplay='block';[/color]

    You should check that the style and display properties are supported
    before using them:

    if( element.style && 'undefined' != element.style.d isplay ) {
    element.style.d isplay = 'block';
    }

    This goes for the hideElement() function, too.

    [snip]

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Randy Webb

      #3
      Re: Problem with hiding/unhiding DIV

      Michael Winter wrote:
      [color=blue]
      > On Sat, 06 Mar 2004 01:10:03 -0500, mcheu <mpcheu@yahoo.c om> wrote:
      >[color=green]
      >> It does all of this just fine in IE5, IE5.5, and IE6. However, in
      >> Mozilla 1.5, the display attribute doesn't get changed, and the
      >> message text remains hidden.[/color]
      >
      >
      > [snip]
      >[color=green]
      >> Is there something different I need to do to get this to work in the
      >> Netscape/Mozilla family of browsers?[/color]
      >
      >
      > The problem is how you call the function:
      >
      > <A HREF="#nowhere" onclick="showEl ement(id1);">Sh ow</A>[/color]


      Or in the function itself. Which I would be more inclined to point at
      instead of the call to the function.

      function showElement(ele ment)
      {
      document.getEle mentById(elemen t).style.displa y='block';
      }

      along with this in the script block:

      if(document.all && !document.getEl ementById) {
      document.getEle mentById = function(id) {
      return document.all[id];
      }
      }

      Still needs the object detection you added, but I prefer to keep my
      function calls to a minimum.

      --
      Randy
      Chance Favors The Prepared Mind
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • Jeff North

        #4
        Re: Problem with hiding/unhiding DIV

        On Sat, 06 Mar 2004 01:10:03 -0500, in comp.lang.javas cript mcheu
        <mpcheu@yahoo.c om> wrote:
        [color=blue]
        >| Hi,
        >|
        >| I'm trying to figure out how to hide text in a DIV block and then hide
        >| and unhide it using javascript. In the code that follows, the message
        >| is initially invisible, and when you click on the "show" link, it's
        >| made visible and pops up bettween the two hyphen lines. When you
        >| click on the "hide" link, it's made invisible again. Or at least
        >| that's what is supposed to happen.
        >|
        >| It does all of this just fine in IE5, IE5.5, and IE6. However, in
        >| Mozilla 1.5, the display attribute doesn't get changed, and the
        >| message text remains hidden.
        >|
        >| A google search of past postings mentions that this doesn't work in
        >| Netscape 4, but it *is* supposed to work in Netscape 6, which I guess
        >| is approximately equivalent to Mozilla 1.0.
        >|
        >| Is there something different I need to do to get this to work in the
        >| Netscape/Mozilla family of browsers?
        >|
        >| Thanks.
        >| MCheu[/color]

        ----------------------------------
        <HTML>
        <HEAD>
        <SCRIPT LANGUAGE="JavaS cript">
        function showElement(ele ment)
        {
        var target = document.getEle mentById(elemen t);
        target.style.di splay='block';
        }
        function hideElement (element)
        {
        var target = document.getEle mentById(elemen t);
        target.style.di splay='none';
        }
        </SCRIPT>
        </HEAD>
        <BODY>
        <A HREF="#nowhere" onclick="showEl ement('id1');"> Show</A>
        <BR>
        <A HREF="#nowhere" onclick="hideEl ement('id1');"> Hide</A>
        <BR><BR>
        --------------------------------------<BR>
        <DIV Name="id1" ID="id1" style="display: block">
        CAN YOU SEE ME?
        </DIV>
        --------------------------------------<BR>
        </HTML>
        ----------------------------------
        ---------------------------------------------------------------
        jnorth@yourpant sbigpond.net.au : Remove your pants to reply
        ---------------------------------------------------------------

        Comment

        • mcheu

          #5
          Re: Problem with hiding/unhiding DIV

          On Sat, 06 Mar 2004 14:29:36 GMT, Jeff North
          <jnorth@yourpan tsbigpond.net.a u> wrote::
          [color=blue]
          >On Sat, 06 Mar 2004 01:10:03 -0500, in comp.lang.javas cript mcheu
          ><mpcheu@yahoo. com> wrote:
          >[color=green]
          >>| Hi,
          >>|
          >>| I'm trying to figure out how to hide text in a DIV block and then hide
          >>| and unhide it using javascript. In the code that follows, the message
          >>| is initially invisible, and when you click on the "show" link, it's
          >>| made visible and pops up bettween the two hyphen lines. When you
          >>| click on the "hide" link, it's made invisible again. Or at least
          >>| that's what is supposed to happen.
          >>|
          >>| It does all of this just fine in IE5, IE5.5, and IE6. However, in
          >>| Mozilla 1.5, the display attribute doesn't get changed, and the
          >>| message text remains hidden.
          >>|
          >>| A google search of past postings mentions that this doesn't work in
          >>| Netscape 4, but it *is* supposed to work in Netscape 6, which I guess
          >>| is approximately equivalent to Mozilla 1.0.
          >>|
          >>| Is there something different I need to do to get this to work in the
          >>| Netscape/Mozilla family of browsers?
          >>|
          >>| Thanks.
          >>| MCheu[/color]
          >
          >----------------------------------
          ><HTML>
          ><HEAD>
          ><SCRIPT LANGUAGE="JavaS cript">
          >function showElement(ele ment)
          >{
          >var target = document.getEle mentById(elemen t);
          >target.style.d isplay='block';
          >}
          >function hideElement (element)
          >{
          >var target = document.getEle mentById(elemen t);
          >target.style.d isplay='none';
          >}
          ></SCRIPT>
          ></HEAD>
          ><BODY>
          ><A HREF="#nowhere" onclick="showEl ement('id1');"> Show</A>
          ><BR>
          ><A HREF="#nowhere" onclick="hideEl ement('id1');"> Hide</A>
          ><BR><BR>
          >--------------------------------------<BR>
          ><DIV Name="id1" ID="id1" style="display: block">
          >CAN YOU SEE ME?
          ></DIV>
          >--------------------------------------<BR>
          ></HTML>
          >----------------------------------
          >---------------------------------------------------------------
          >jnorth@yourpan tsbigpond.net.a u : Remove your pants to reply
          >---------------------------------------------------------------[/color]

          Thanks. The test page works fine now with the changes.
          ----------------------------------------
          Thanks,
          MCheu

          Comment

          • mcheu

            #6
            Re: Problem with hiding/unhiding DIV

            On Sat, 06 Mar 2004 12:31:32 GMT, Michael Winter
            <M.Winter@bluey onder.co.invali d> wrote::
            [color=blue]
            >On Sat, 06 Mar 2004 01:10:03 -0500, mcheu <mpcheu@yahoo.c om> wrote:
            >[color=green]
            >> It does all of this just fine in IE5, IE5.5, and IE6. However, in
            >> Mozilla 1.5, the display attribute doesn't get changed, and the
            >> message text remains hidden.[/color]
            >
            >[snip]
            >[color=green]
            >> Is there something different I need to do to get this to work in the
            >> Netscape/Mozilla family of browsers?[/color]
            >
            >The problem is how you call the function:
            >
            > <A HREF="#nowhere" onclick="showEl ement(id1);">Sh ow</A>
            >
            >Here, you treat id1, the id of the DIV element, as a global variable. IE,
            >and some other browsers, honour this, but there is no reason why they
            >should. That's why stricter Gecko-based browsers fail. Instead, use a
            >function like:
            >
            > function getById( id ) {
            > if( document.getEle mentById ) {
            > return document.getEle mentById( id );
            > } else if( document.all ) {
            > return document.all[ id ];
            > }
            > return null;
            > }
            >
            >and call the hide/show functions like:
            >
            > <A HREF="#nowhere" onclick="showEl ement(getById(' id1'));">Show</A>
            >
            >This should now work on browsers that support the style property and the
            >document.getEl ementById() method or .all[] collection. That should account
            >for the vast majority of modern browsers.
            >
            >[snip]
            >[color=green]
            >> <SCRIPT LANGUAGE="JavaS cript">[/color]
            >
            >The language attribute is deprecated. Use the type attribute instead (it's
            >required anyway):
            >
            > <script type="text/javascript">
            >[color=green]
            >> <!--[/color]
            >
            >The practice of hiding scripts is now obsolete. Remove the SGML comment
            >delimiters.
            >[color=green]
            >> function showElement(ele ment)
            >> {
            >> //element.style.d isplay='';
            >> element.style.d isplay='block';[/color]
            >
            >You should check that the style and display properties are supported
            >before using them:
            >
            > if( element.style && 'undefined' != element.style.d isplay ) {
            > element.style.d isplay = 'block';
            > }
            >
            >This goes for the hideElement() function, too.
            >
            >[snip]
            >
            >Mike[/color]

            Thanks for the very informative reply.

            ----------------------------------------
            Thanks,
            MCheu

            Comment

            • Jeff North

              #7
              Re: Problem with hiding/unhiding DIV

              On Sat, 06 Mar 2004 15:16:29 -0500, in comp.lang.javas cript mcheu
              <mpcheu@yahoo.c om> wrote:

              [snip]
              [color=blue]
              >| Thanks. The test page works fine now with the changes.[/color]

              Just a reminder, this is for DOM2 compliant browsers. Randy and
              Michael have posted a complete solution.
              ---------------------------------------------------------------
              jnorth@yourpant sbigpond.net.au : Remove your pants to reply
              ---------------------------------------------------------------

              Comment

              • Randy Webb

                #8
                Re: Problem with hiding/unhiding DIV

                Jeff North wrote:[color=blue]
                > On Sat, 06 Mar 2004 15:16:29 -0500, in comp.lang.javas cript mcheu
                > <mpcheu@yahoo.c om> wrote:
                >
                > [snip]
                >
                >[color=green]
                >>| Thanks. The test page works fine now with the changes.[/color]
                >
                >
                > Just a reminder, this is for DOM2 compliant browsers. Randy and
                > Michael have posted a complete solution.[/color]

                Not sure that a "complete" solution would exist. None of the solutions
                so far have included document.layers . The only benefit added by Michael
                or myself was IE4 compatability (to an extent)

                --
                Randy
                Chance Favors The Prepared Mind
                comp.lang.javas cript FAQ - http://jibbering.com/faq/

                Comment

                • Jeff North

                  #9
                  Re: Problem with hiding/unhiding DIV

                  On Sat, 06 Mar 2004 23:22:20 -0500, in comp.lang.javas cript Randy Webb
                  <hikksnotathome @aol.com> wrote:
                  [color=blue]
                  >| Jeff North wrote:
                  >| > On Sat, 06 Mar 2004 15:16:29 -0500, in comp.lang.javas cript mcheu
                  >| > <mpcheu@yahoo.c om> wrote:
                  >| >
                  >| > [snip]
                  >| >
                  >| >
                  >| >>| Thanks. The test page works fine now with the changes.
                  >| >
                  >| >
                  >| > Just a reminder, this is for DOM2 compliant browsers. Randy and
                  >| > Michael have posted a complete solution.
                  >|
                  >| Not sure that a "complete" solution would exist. None of the solutions
                  >| so far have included document.layers . The only benefit added by Michael
                  >| or myself was IE4 compatability (to an extent)[/color]

                  Well your response was more 'complete' than mine :-)

                  ---------------------------------------------------------------
                  jnorth@yourpant sbigpond.net.au : Remove your pants to reply
                  ---------------------------------------------------------------

                  Comment

                  Working...