Results 1 to 9 of 9

Thread: AnyLink Drop Down Menu - onclick issue

  1. #1
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AnyLink Drop Down Menu - onclick issue

    1) Script Title:

    AnyLink Drop Down Menu

    2) Script URL (on DD):

    http://www.dynamicdrive.com/dynamici...pmenuindex.htm

    3) Describe problem:

    I'm using a slightly modified code of this script. What I currently have are two (or more) links on a page that each show a div when the user clicks on them. The divs dissappear when the user clicks anywhere else in the browser's window, or on other links. What I need to do is to further modify the code so that the divs of each link also dissapear when the users clicks on the same link again. For example, clicking link1 should show div1, but when the user re-clicks again on link1, the div should dissapear.

    My current script is here: http://www.eurovisionclub.com/memberstest.html

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    This might require some additional tweaking, but try the below modified version of the script. Changes in red:

    Code:
    <html><head>
    	<title>Test</title>
    
    	<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    
    
    <style type="text/css">
    
    #dropmenudiv{
    position:absolute;
    border:1px solid black;
    border-bottom-width: 1;
    font:normal 11px Verdana;
    padding: 5px 5px 5px 5px;
    line-height:14px;
    z-index:100;
    }
    
    #dropmenudiv a{
    width: 100%;
    display: block;
    text-indent: 3px;
    border-bottom: 1px solid black;
    padding: 1px 0;
    text-decoration: none;
    font-weight: bold;
    }
    
    #dropmenudiv a:hover{ /*hover background color*/
    background-color: yellow;
    }
    
    </style>
    
    <script type="text/javascript">
    
    /***********************************************
    * AnyLink Drop Down Menu- © Dynamic Drive (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/
    
    
    function show_member_name(member_name)
    {
        return '<strong><font size=+2>' + member_name + '</strong></font>';
    }
    
    
    
    
    function drawline()
    {
        return '<hr style="color: #FFCC66; height: 1px; text-align: left; width: 96%;">'
    }
    
    //Contents for menu 1
    
    var menu0=new Array()
    menu0[0]='' + drawline() + '' + show_member_name('test1') + drawline() + '</font>'
    var menu1=new Array()
    menu1[0]='' + drawline() + '' + show_member_name('test2') + drawline() + '</font>'
    var menu2=new Array()
    
    
    		
    var menuwidth='165px' //default menu width
    var menubgcolor='lightyellow'  //menu bgcolor
    var disappeardelay=250  //menu disappear speed onMouseout (in miliseconds)
    var hidemenu_onclick="yes" //hide menu when user clicks within menu?
    
    /////No further editting needed
    
    var ie4=document.all
    var ns6=document.getElementById&&!document.all
    
    if (ie4||ns6)
    document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';background-color:'+menubgcolor+'" onMouseover="clearhidemenu()"></div>')
    
    function getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
    }
    return totaloffset;
    }
    
    
    function showhide(obj, e, visible, hidden, menuwidth){
    if (ie4||ns6)
    dropmenuobj.style.left=dropmenuobj.style.top="-500px"
    if (menuwidth!=""){
    dropmenuobj.widthobj=dropmenuobj.style
    dropmenuobj.widthobj.width=menuwidth
    }
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
    obj.visibility=visible
    else if (e.type=="click")
    obj.visibility=hidden
    }
    
    function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    }
    
    function clearbrowseredge(obj, whichedge){
    var edgeoffset=0
    if (whichedge=="rightedge"){
    var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
    if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
    edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
    }
    else{
    var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
    var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
    if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
    edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
    if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
    edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
    }
    }
    return edgeoffset
    }
    
    function populatemenu(what){
    if (ie4||ns6)
    dropmenuobj.innerHTML=what.join("")
    }
    
    
    function dropdownmenu(obj, e, menucontents, menuwidth){
    
    var menustate = obj.getAttribute('data-menustate')
    
    if (window.event) event.cancelBubble=true
    else if (e.stopPropagation) e.stopPropagation()
    clearhidemenu()
    dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
    populatemenu(menucontents)
    
    if (menustate == "hidden"){
    if (ie4||ns6){
    showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
    dropmenuobj.x=getposOffset(obj, "left") + 40
    dropmenuobj.y=getposOffset(obj, "top") - 25
    dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
    dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
    }
    }
    else{
    hidemenu(e)
    }
    
    obj.setAttribute("data-menustate", (menustate=="hidden")? "visible" : "hidden")
    
    return clickreturnvalue()
    }
    
    function clickreturnvalue(){
    if (ie4||ns6) return false
    else return true
    }
    
    function contains_ns6(a, b) {
    while (b.parentNode)
    if ((b = b.parentNode) == a)
    return true;
    return false;
    }
    
    function dynamichide(e){
    if (ie4&&!dropmenuobj.contains(e.toElement))
    delayhidemenu()
    else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
    delayhidemenu()
    }
    
    function hidemenu(e){
    if (typeof dropmenuobj!="undefined"){
    if (ie4||ns6)
    dropmenuobj.style.visibility="hidden"
    }
    }
    
    function delayhidemenu(){
    if (ie4||ns6)
    delayhide=setTimeout("hidemenu()",disappeardelay)
    }
    
    function clearhidemenu(){
    if (typeof delayhide!="undefined")
    clearTimeout(delayhide)
    }
    
    if (hidemenu_onclick=="yes")
    document.onclick=hidemenu
    
    </script></head><body><div id="dropmenudiv" style="visibility: hidden; width: 400px; background-color: lightyellow; top: 63px; left: 48px;" onmouseover="clearhidemenu()"><hr style="color: #FFCC66; height: 1px; text-align: left; width: 96%;"><strong><font size="+2">test2</font></strong><hr style="color: #FFCC66; height: 1px; text-align: left; width: 96%;"></div>
    
    
    
    
    
    
    
    <br>
    
    
    <a href="#" onclick="hidemenu();return dropdownmenu(this, event, menu0, '400px')" data-menustate="hidden">link 1</a>
    
    <br><br>
    
    <a href="#" onclick="hidemenu();return dropdownmenu(this, event, menu1, '400px')" data-menustate="hidden">link 2</a>
    
    
    
    
    </body></html>
    DD Admin

  3. #3
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It worked, but it is a bit buggy

    The problem is that now, it needs 2 clicks to make a div appear again after it has dissappeard.

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Ok, in addition to the above changes, try also adding the new part in red below inside your <a> elements:

    Code:
    <a href="#" onclick="hidemenu();return dropdownmenu(this, event, menu0, '400px')" data-menustate="hidden" onBlur="this.setAttribute('data-menustate', 'hidden')">link 1</a>
    
    <br><br>
    
    <a href="#" onclick="hidemenu();return dropdownmenu(this, event, menu1, '400px')" data-menustate="hidden" onBlur="this.setAttribute('data-menustate', 'hidden')">link 2</a>
    DD Admin

  5. #5
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    it's still buggy when clicking between the links

    http://www.eurovisionclub.com/test.html

  6. #6
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    I see the issue, but only in IE9 (versus say FF). Give your document a valid doctype at the very top, for example, by adding the following:

    Code:
    <!doctype html>
    DD Admin

  7. #7
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It doesn't work properly in Google Chrome either, even with a doctype. http://www.eurovisionclub.com/test.html

  8. #8
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Hmm it seems Chrome doesn't fire the onBlur event handler on general elements on the page unless it's accompanied by a "tabindex" attribute inside the element. So try defining one for each of your anchor links:

    Code:
    <a href="#" onclick="hidemenu();return dropdownmenu(this, event, menu0, '400px')" data-menustate="hidden" tabindex="0" onBlur="this.setAttribute('data-menustate', 'hidden')">link 1</a>
    
    <br><br>
    
    <a href="#" onclick="hidemenu();return dropdownmenu(this, event, menu1, '400px')" data-menustate="hidden" tabindex="1" onBlur="this.setAttribute('data-menustate', 'hidden')">link 2</a>
    DD Admin

  9. #9
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It now works great in both Chrome and Firefox - thanks!!

    The only thing that is missing now is to make it work with IE. I tested it in IE6 and unfortunately it doesn't work properly. I know, it's an out-of-date browser but many organisations still use it, so if the script can me modified to work with that, it would really be nice. The problem with IE6 is when clicking the same link several times - after a couple of clicks it doesn't do anything, unless you click on it again. The updated code is here: http://www.eurovisionclub.com/test.html

Similar Threads

  1. AnyLink Drop Down Menu - onclick issue
    By billgates2000 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 04-23-2008, 01:00 PM
  2. AnyLink Drop Down Menu - border issue *and* FF alignment issue
    By trjonas in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 12-18-2007, 05:58 AM
  3. AnyLink Drop Down Menu - IE Issue
    By the black mambo in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 06-27-2007, 07:59 PM
  4. AnyLink Drop Down Menu - onClick version
    By affu2000 in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 12-13-2005, 07:17 PM
  5. a.dropMenu:hover not working w/onClick & AnyLink Drop Down Menu
    By jbrenny in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 04-28-2005, 09:10 PM

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
  •