Results 1 to 4 of 4

Thread: Simple javascript menu

  1. #1
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple javascript menu

    Hey guys I'm not a javascript expert, but I've got this script kina working and I need some help finishing it. I'm using this to hide and display content in a small area. What am I doing wrong? Here's my test script:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    <style>
    #div1{display:block;position:absolute;width:600px;top:75px;left:20px;}
    #div2{display:none;position:absolute;width:600px;top:75px;left:20px;}
    #div3{display:none;position:absolute;width:600px;top:75px;left:20px;}
    </style>
    
    <script type="text/javascript">
    function Hide(id){
      document.getElementById(id).style.display = "none";
    }
    
    function Show(id) {
      document.getElementById(id).style.display = "block";
    }
    </script>
    
    </head>
    
    <body>
    
    <div>
    <a href="#" onClick="Show('div1'); Hide('div2','div3');">Why Brush Country</a>
    <a href="#" onClick="Show('div2'); Hide('div1','div3');">What We Do</a>
    <a href="#" onClick="Show('div3'); Hide('div1','div2');">Why Brush Country</a>
    </div>
    
    <div style="position:relative">
    
    <div id="div1">			
    content one				
    </div>
    					
    <div id="div2">			
    content two		
    </div>
    						
    <div id="div3">
    content three
    </div>
    
    </div>
    
    </body>
    </html>

  2. #2
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    P.S. Just ignore the button titles

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Well, the most apparent thing to me is the following:

    Quote Originally Posted by myyoungfamily

    Code:
    <a href="#" onClick="Show('div1'); Hide('div2','div3');">Why Brush Country</a>
    and


    [
    Code:
    function Hide(id){
      document.getElementById(id).style.display = "none";
    }
    Notice, you are calling for mulitple IDs to be hidden, but in the Hide function, you only have one ID that can be. You could either add another in the hide function like so:

    Code:
    function Hide(id,other) {
       document.getElementById(id).style.display = "none";
       document.getElementById(other).style.display = "none";
    }
    or simply rewrite it to only show the one and hide the rest with a "for" loop. See below.

    Use the following for each link:

    Code:
    <a href="#" onClick="ToggleDivs('div1'); return false">Show Div One</a>
    and in the head of the document, place the following within the script tag:

    Code:
    function ToggleDivs(id) {
    var divs = document.getElementsByTagName('div');
    var d = document.getElementById(id);
    
    	for (var i = 1; i<=divs.length; i++) {
    		if (document.getElementById('div'+i)) {document.getElementById('div'+i).style.display='none';}
    	}
    if (d) {d.style.display='block';}
    }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Jul 2005
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PERFECT! Just what I was needing...

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
  •