1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript down down menu

Discussion in 'JavaScript' started by dean5000v, Feb 24, 2008.

  1. #1
    hey ok well i have made a drop down menu in css and html i have added in this javascript in order to open up a tab when clicked on:

    
    <!--
    window.onload=montre;
    function montre(id) {
    var d = document.getElementById(id);
    	for (var i = 1; i<=10; i++) {
    		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
    	}
    if (d) {d.style.display='block';}
    }
    //-->
    </script>
    
    Code (markup):
    And i also have this javascript code to close the tab

    <dt onclick="javascript:montre();">
    Code (markup):
    the problem with this is that you can't click on the tab to open it and then click it again to close i have to use a different tab with the javascript in it to close, does anyone know any other ways to get around this.
     
    dean5000v, Feb 24, 2008 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Try it like this:
    if (d && d.style) {d.style.display=(d.style.display!='block'?'block':'none');}
    Code (markup):
     
    Logic Ali, Feb 24, 2008 IP
  3. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ive tried that but it hasn't worked anyone else have any other javascript i could possible add in instead or mybe some over entire scripts for the job. thanks
     
    dean5000v, Feb 24, 2008 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Can you give a URL to a test page?
     
    Logic Ali, Feb 24, 2008 IP
  5. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    here is this link: http://www.weblayoutsrus.com/dormerfinance/index.html
    thanks for the help.
     
    dean5000v, Feb 25, 2008 IP
  6. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #6
    The code I gave does work, now I have all the code I see it's being countered by the action of the for loop, which acts on 'smenu'+? when there is only one matching element, and that is assigned to d. You need to re-think what the code is doing.

    BTW all IDs in a document must be unique.
     
    Logic Ali, Feb 25, 2008 IP
  7. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i know this is cheeky is there anyway u could tell me how i could mybe fix this problem then ?
     
    dean5000v, Feb 25, 2008 IP
  8. Lisans

    Lisans Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <script type="text/javascript">
    
    <!--
    
    window.onload=montre;
    
    function montre(id) {
    
    var d = document.getElementById(id);
    
    
    	
    
    if (d) {d.style.display = (d.style.display == 'block')? 'none': 'block';}
    
    for (var i = 1; i<=10; i++) {
    
    		if (document.getElementById('smenu'+i) && document.getElementById('smenu'+i) != d) {document.getElementById('smenu'+i).style.display='none';}
    
    	}
    
    }
    
    //-->
    
    </script>
    PHP:
     
    Lisans, Feb 25, 2008 IP