www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 08-19-2005, 12:32 PM
    emma emma is offline
    Registered User
     
    Join Date: May 2004
    Posts: 23
    resolved [RESOLVED] show / hide

    I have a FAQ's section on a webpage and I want the page to have the questions in a list and when a user clicks the question the answer will appear show underneath that question. Also when the user clicks the question again the answer will hide.

    Can anyone send me the Javascript code that will do this please?
    Reply With Quote
      #2  
    Old 08-19-2005, 12:50 PM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 21,679
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>hide or show</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    <!--
     function toggle(obj) {
    // Moz. or IE
    var sibling=(obj.nextSibling.nodeType==3)? obj.nextSibling.nextSibling : obj.nextSibling;
    // hide or show
    if(sibling.style.display=='' || sibling.style.display=='block') {
    	sibling.style.display='none';
        obj.firstChild.firstChild.data='+';
        }
    else {
    	sibling.style.display='block';
        obj.firstChild.firstChild.data='-';
        }
    }
    //
    function initCollapse() {
    var oDT=document.getElementById('content').getElementsByTagName('dt');
    for (var i=0; i < oDT.length; i++) {
    	oDT[i].onclick=function() {toggle(this)};
        var oSpan=document.createElement('span');
        var sign=document.createTextNode('+');
        oSpan.appendChild(sign);
        oDT[i].insertBefore(oSpan, oDT[i].firstChild);
        oSpan.style.fontFamily='monospace';
        oSpan.style.paddingRight='0.5em';
        oDT[i].style.cursor='pointer';
        toggle(oDT[i]);
    	}
    oDT=null;
    }
    window.onload=function() {if(document.getElementById && document.createElement) {initCollapse();}}
    //-->
    </script>
    
    <style type="text/css">
    <!--
    body{
    background:#ccc;
    font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;
    font-size:1em;
    }
    -->
    </style>
    
    </head>
    <body>
    <dl id="content">
        <dt>Headline 1</dt>
        	<dd>Content 1</dd>
        <dt>Headline 2</dt>
        	<dd>Content 2</dd>
    </dl>
    </body>
    </html>
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #3  
    Old 08-22-2005, 04:50 AM
    emma emma is offline
    Registered User
     
    Join Date: May 2004
    Posts: 23
    Excellent - thanks for that it works just as I wanted it to.
    Reply With Quote
      #4  
    Old 08-24-2005, 03:54 PM
    Dorisanna Dorisanna is offline
    Registered User
     
    Join Date: Aug 2005
    Posts: 1
    Exclamation

    hello,

    My question is... How do you seperate the FAQ's into categories?
    Example:

    About SharePoint Portal
    What is sharePoint Portal Server 2003?
    How will using SharePoint benefit my department?

    Login and access issues
    How do I login?

    I've tried the script that you have listed and it's wonderful, but I am having a hard time seperating the different categories. It basically puts all the questions together so you can't categorize them.
    Help please!!!

    Thank you!

    Last edited by Dorisanna; 08-24-2005 at 04:02 PM.
    Reply With Quote
      #5  
    Old 04-18-2006, 02:42 PM
    chet23 chet23 is offline
    Registered User
     
    Join Date: Apr 2006
    Posts: 6
    Is there a simple way to have a selected element appear expanded onLoad?
    Reply With Quote
      #6  
    Old 04-18-2006, 04:07 PM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 21,679
    It depends how the index to the menu is passed to the document.
    This is one possibility:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>hide or show</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
     function toggle(obj) {
    // Moz. or IE
    var sibling=(obj.nextSibling.nodeType==3)? obj.nextSibling.nextSibling : obj.nextSibling;
    // hide or show
    if(sibling.style.display=='' || sibling.style.display=='block') {
    	sibling.style.display='none';
        obj.firstChild.firstChild.data='+';
        }
    else {
    	sibling.style.display='block';
        obj.firstChild.firstChild.data='-';
        }
    }
    //
    function initCollapse() {
    var oDT=document.getElementById('content').getElementsByTagName('dt');
    for (var i=0; i < oDT.length; i++) {
    	oDT[i].onclick=function() {toggle(this)};
        var oSpan=document.createElement('span');
        var sign=document.createTextNode('+');
        oSpan.appendChild(sign);
        oDT[i].insertBefore(oSpan, oDT[i].firstChild);
        oSpan.style.fontFamily='monospace';
        oSpan.style.paddingRight='0.5em';
        oDT[i].style.cursor='pointer';
        toggle(oDT[i]);
    	}
    oDT=null;
    }
    window.onload=function() {
    if(document.getElementById && document.createElement) {
        initCollapse();
        var openDT=document.body.id.charAt(document.body.id.length-1)
         toggle(document.getElementById('content').getElementsByTagName('dt')[openDT]);
        }
    }
    </script>
    
    <style type="text/css">
    body{
    background:#ccc;
    font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;
    font-size:1em;
    }
    </style>
    
    </head>
    <body id="index1">
    <dl id="content">
        <dt>Headline 1</dt>
        	<dd>Content 1</dd>
        <dt>Headline 2</dt>
        	<dd>Content 2</dd>
        <dt>Headline 3</dt>
        	<dd>Content 3</dd>
    </dl>
    </body>
    </html>
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #7  
    Old 04-18-2006, 04:27 PM
    slaughters slaughters is offline
    Doo Wah Diddy
     
    Join Date: Apr 2006
    Location: Houston
    Posts: 1,372
    Something like this would be simpler
    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <script>
     function OpenClose(obj) {
       if (obj.style.display=='block') {
         obj.style.display='none';
       } else {
         obj.style.display='block';
       }
     }
    </script>
    </head>
    
    <body>
    
    <div id="ClickMe_Head" onClick="OpenClose(ClickMe_Body)" onMouseOver="this.style.cursor='pointer';">
     Click Me
    </div>
    <div id="ClickMe_Body" style="display:none;">
     Blah blah blah bla<br>
     blah blah blah bla<br>
     Blah blah blah bla<br>
     blah blah blah bla
    </div>
    
    </body>
    </html>
    Reply With Quote
      #8  
    Old 04-18-2006, 04:40 PM
    chet23 chet23 is offline
    Registered User
     
    Join Date: Apr 2006
    Posts: 6
    Oh wow, that's beautiful. Your script's a little beyond my ability to fully grok, but its great. Thank you so much for your quick response. It does exactly what I was hoping it would.I thopight an ID needed to be added somewhere, but wasn't sure how to proceed.

    What sort of modifications would be required to lock a DD element open on a given page (keep it from closing)? And if one wanted to close other DDs when another opened? These aren't necessary for my purposes, but would round out this script nicely.. Might even shed some light on how its working. Right now I grok about 85% of what's going on there.

    Thanks again, Fang, for your help.Your are a gentleman (woman?) and a scholar. May you live long and prosper.

    Sincerely,
    a much indebted Chet23
    Reply With Quote
      #9  
    Old 04-18-2006, 04:44 PM
    chet23 chet23 is offline
    Registered User
     
    Join Date: Apr 2006
    Posts: 6
    Slaughters,
    Thanks for your input. That works great, but I especially like the way Fang's script uses the '+' and '-' before the section headers. Makes the links function like a tree, which is what I was specifically looking for.
    -Chet
    Reply With Quote
      #10  
    Old 04-18-2006, 04:59 PM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 21,679
    Moving the goalposts ...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>hide or show</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
     function toggle(obj) {
    // Moz. or IE
    var sibling=(obj.nextSibling.nodeType==3)? obj.nextSibling.nextSibling : obj.nextSibling;
    // hide or show
    if(sibling.style.display=='' || sibling.style.display=='block') {
    	sibling.style.display='none';
        obj.firstChild.firstChild.data='+';
        }
    else {
    	sibling.style.display='block';
        obj.firstChild.firstChild.data='-';
        }
    }
    //
    function initCollapse() {
    var oDT=document.getElementById('content').getElementsByTagName('dt');
    var openDT=document.body.id.charAt(document.body.id.length-1);
    for (var i=0; i < oDT.length; i++) {
        if(i!=openDT) {
            oDT[i].onclick=function() {toggle(this)};
            var oSpan=document.createElement('span');
            var sign=document.createTextNode('+');
            oSpan.appendChild(sign);
            oDT[i].insertBefore(oSpan, oDT[i].firstChild);
            oSpan.style.fontFamily='monospace';
            oSpan.style.paddingRight='0.5em';
            oDT[i].style.cursor='pointer';
            toggle(oDT[i]);
        	}
        else {
            var oSpan=document.createElement('span');
            var sign=document.createTextNode('-');
            oSpan.appendChild(sign);
            oDT[i].insertBefore(oSpan, oDT[i].firstChild);
            oSpan.style.fontFamily='monospace';
            oSpan.style.paddingRight='0.5em';
            }
        }
    oDT=null;
    }
    window.onload=function() {
    if(document.getElementById && document.createElement) {
    	initCollapse();
        }
    }
    </script>
    
    <style type="text/css">
    body{
    background:#ccc;
    font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;
    font-size:1em;
    }
    </style>
    
    </head>
    <body id="index1">
    <dl id="content">
        <dt>Headline 1</dt>
        	<dd>Content 1</dd>
        <dt>Headline 2</dt>
        	<dd>Content 2</dd>
        <dt>Headline 3</dt>
        	<dd>Content 3</dd>
    </dl>
    </body>
    </html>
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #11  
    Old 04-18-2006, 08:30 PM
    chet23 chet23 is offline
    Registered User
     
    Join Date: Apr 2006
    Posts: 6
    Thanks again, Fang. I swear I wasn't moving goalposts - i was just curious.

    I do have another question now though. I actually stumbled across a solution earlier, but don't know where - can't find it now. When i load a page with this script, the whole expanded list appears for a quick second then collapses to where it should be. The fix I had found said something about adding something just before the closing body tag of each doc. Do you know what I'm talkihng about? Do you have a solution?

    Thanks in advance for your help. If there's anything I can do to rteturn the favor, let me know.

    -Chet
    Reply With Quote
      #12  
    Old 04-19-2006, 01:52 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 21,679
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #13  
    Old 04-19-2006, 08:09 AM
    slaughters slaughters is offline
    Doo Wah Diddy
     
    Join Date: Apr 2006
    Location: Houston
    Posts: 1,372
    Quote:
    Originally Posted by chet23
    Slaughters,
    Thanks for your input. That works great, but I especially like the way Fang's script uses the '+' and '-' before the section headers. Makes the links function like a tree, which is what I was specifically looking for.
    -Chet
    If it fits your needs better then great, but I still think it is overly complexed code for a very simple task.

    P.S.

    Plus and minus signs are easy enough to add
    Link: http://www.stansight.com/temp.html
    Code:
    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <script>
     function OpenClose(obj) {
       HeadObj = eval(obj + "_Head");
       BodyObj = eval(obj + "_Body");
    
       if (BodyObj.style.display=='block') {
         BodyObj.style.display='none';
         SignOffset = HeadObj.innerHTML.indexOf("-");
         HeadObj.innerHTML = "+" + HeadObj.innerHTML.substring(SignOffset+1);
       } else {
         BodyObj.style.display='block';
         SignOffset = HeadObj.innerHTML.indexOf("+");
         HeadObj.innerHTML = "-" + HeadObj.innerHTML.substring(SignOffset+1);
       }
     }
    </script>
    </head>
    
    <body>
    
    <div id="ClickMe1_Head" onClick="OpenClose('ClickMe1')" onMouseOver="this.style.cursor='pointer';" style="color: #000099;">
    + FAQ # 1
    </div>
    <div id="ClickMe1_Body" style="display:none; margin-left:20px">
     That's a FAQ Jack ! That's a FAQ Jack ! That's a FAQ Jack !
    </div>
    
    <div id="ClickMe2_Head" onClick="OpenClose('ClickMe2')" onMouseOver="this.style.cursor='pointer';" style="color: #000099;">
    + FAQ # 2
    </div>
    <div id="ClickMe2_Body" style="display:none; margin-left:20px">
     That's a FAQ Jack ! That's a FAQ Jack ! That's a FAQ Jack !
    </div>
    </body>
    </html>
    Reply With Quote
      #14  
    Old 04-19-2006, 10:42 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 21,679
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees
    Reply With Quote
      #15  
    Old 04-19-2006, 11:44 AM
    slaughters slaughters is offline
    Doo Wah Diddy
     
    Join Date: Apr 2006
    Location: Houston
    Posts: 1,372
    Fang - The links you posted are written by people who:

    (a) Opinions I disagree with, or
    (b) Have very narrowly focused points of view

    Let's get practical. Point to a browser which would have issues with the code I posted.

    Here is good link to world wide web browser usage statistics for March 2006: http://www.thecounter.com/stats/2006/March/browser.php This code should work for every browser that has a usage of 1% or greater. If not let me know. If you can't find one, or if it is so obscure that only 0.001 % of the people use it, then I really don't see the point.

    My style is to write easy to understand and maintain code that works for the specific targeted group (in this case IE and Firefox browsers). Cryptic code that supports everything and everyone just leads to headaches for the people who have to follow behind you and maintain it.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:11 PM.


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.