Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    217
    Thanks
    25
    Thanked 1 Time in 1 Post

    hide/show div problem

    I made a div hide/show script for a friend, but he complains it's not working in Firefox & Chrome. It works fine in IE though..

    What do I need to alter to make this work?

    Code:
    <script type=text/javascript>
    function toggleT(_w,_h) {
    if (document.all) { // is IE
    if (_h=='s') eval("document.all."+_w+".style.visibility='visible';");
    if (_h=='h') eval("document.all."+_w+".style.visibility='hidden';");
    } else { // is NS? 
    if (_h=='s') eval("document.layers['"+_w+"'].visibility='show';");
    if (_h=='h') eval("document.layers['"+_w+"'].visibility='hide';");
    }
    }
    </script>
    
    <span id='divt2' style='visibility:visible;position:relative;top:0;left:0'><p>
    Here is some text
    </span></p><br><input type='submit' value='bestel' onClick=toggleT('divt1','s');toggleT('divt2','h')></span>
    <span id="divt1" style="visibility:hidden;position:absolute;top:10;left:10">
    <form action='klantenorder.php' method='post'>
    Voorwaarden:<br>
    ..tekst..<br><br>
    <input type="hidden" name="couponcode" value="coup01">
    <input type="hidden" name="emailklant" value="spellenkennis@live.nl">
    <input type="submit" value="bestelling plaatsen">
    
    </form>
    </span>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    document.all and document.layers are long obsolete (as is Netscape). Use document.getElementById() instead - supported by all modern browsers.
    IE still supports document.all for historic reasons.

    eval is also regarded as poor practice and is completely unnecessary.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Here's a nice idiot-proof replacement function:

    Code:
    function toggleT( id, action ) 
    {
      var show = { h : 'hidden', s : 'visible' }[ typeof action == 'string' ? action.toLowerCase() : '' ] || alert( 'Second parameter must be "h" or "s" only' ),
          elem = document.getElementById( id ) || alert( 'No rendered element with ID: "' + id  + '"' );
      
      if( elem )    
        elem.style.visibility = show;
    }

  • Users who have thanked Logic Ali for this post:

    docock (11-16-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •