Results 1 to 4 of 4

Thread: Show/Hide code help

  1. #1
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Show/Hide code help

    Hi i need some help with editing show/hide code it is works fine with me but it is show the content every time i refresh the page what i want is to be the conetent hidden till i click on the link to show the content so every time i refersh the page no thing appear untill i click on the show link.

    Code:
    <head>tag
    <script type="text/javascript">
    <!--
    var storedDiv = null;
    function getDiv(oID) {
    if(document.getElementById) {
    return document.getElementById(oID);
    } else if( document.all ) {
    return document.all[oID];
    } else { return null; }
    }
    window.onload = function () {
    for( var i = 0, y; y = getDiv('ans'+i); i++ ) {
    y.style.display = 'none'
    }
    };
    function toggleInfo(oID) {
    var oDiv = getDiv(oID); if( !oDiv ) { return; }
    oDiv.style.display = (oDiv.style.display=='none') ? 'block' : 'none'
    if( storedDiv && storedDiv != oDiv ) { storedDiv.style.display = 'none'
    } storedDiv = oDiv;
    }
    //--></script>
    
    
    in <body>tag
    <span style="cursor: hand; cursor: pointer" onclick="toggleInfo('ans0');">
    >> Hide/show content
    </span><br><br>
    <div id="ans0">
    
    content
    
    </div>
    Last edited by Snookerman; 12-22-2009 at 01:08 PM. Reason: please use [code] tags

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Set the display to none in the stylesheet.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    the onload function will hide the content each time the page is loaded or refreshed.

    Is that what you want??

    NOTE
    setting display to none in the stylesheet will do nothing more and result in poor accessability if javascript is disabled.
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Setting the display to none will ensure that the page need not complete loading before the items are hidden (display:none;). If accessibility is an issue (and it should be, my last post on this was the 'quick answer'), one may create a style section or link using javascript, so that only javascript enabled users will get that treatment. Here's one possible way:

    Code:
    <head>
    <script type="text/javascript">
    if(document.getElementById || document.all)
    document.write('<style type="text/css">.showHide {display:none;}<\/style>');
    var storedDiv = null;
    function getDiv(oID) {
    if(document.getElementById) {
    return document.getElementById(oID);
    } else if( document.all ) {
    return document.all[oID];
    } else { return null; }
    }
    window.onload = function () {
    for( var i = 0, y; y = getDiv('ans'+i); i++ ) {
    y.style.display = 'none'
    }
    };
    function toggleInfo(oID) {
    var oDiv = getDiv(oID); if( !oDiv ) { return; }
    oDiv.style.display = (oDiv.style.display=='none') ? 'block' : 'none'
    if( storedDiv && storedDiv != oDiv ) { storedDiv.style.display = 'none'
    } storedDiv = oDiv;
    }
    </script>
    </head>
    <body>
    <span style="cursor: hand; cursor: pointer" onclick="toggleInfo('ans0');">
    >> Hide/show content
    </span><br><br>
    <div class="showHide" id="ans0">
    content
    </div>
    Give the "showHide" class to all the scripted items (ans#) that you want to have a display of none.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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