/    Sign up×
Community /Pin to ProfileBookmark

Simple horizontal menu problem(s)

The following is a very simple horizontal menu, but it does not do quite what I want.

I see two problems: ?

  • 1.

    I thought surrounding the sub menu <DIV> sections inside a <DIV> would work for the ‘onmouseout’, but it does not. ‘onmouseover’ works fine to display the submenus but they remain forever. Any ideas what to change here?

  • 2.

    I thought the width statement in the ‘mainMenu’ class would stretch the links display, but it does not seem to have any effect on the display. I also tried to surround the links in a <SPAN>, but again, no change to the display width. I know it’s the CSS, but I seem to be missing something here.

  • [code]
    <html>
    <head>
    <title>Simple Menu</title>
    <style type=”text/css”>
    .mainMenu { width:150px; }
    .subMenu { display:none; }
    </style>
    <script type=”text/javascript”>
    var SubMenus = [‘sub1′,’sub2′,’sub3’];
    function hide() {
    for (var i=0; i<SubMenus.length; i++) { document.getElementById(SubMenus[i]).style.display = ‘none’; }
    }
    function show(IDS) { // hide(); // test to remove display
    document.getElementById(IDS).style.display = ‘block’;
    }
    </script>
    </head>
    <body>
    <div id=”topMenu” onmouseout=”hide()”>
    <a href=”#” onmouseover=”show(‘sub1’)”><span class=”mainMenu”>Top 1</span></a>
    <a href=”#” onmouseover=”show(‘sub2’)”><span class=”mainMenu”>Top 2</span></a>
    <a href=”#” onmouseover=”show(‘sub3’)”><span class=”mainMenu”>Top 3</span></a>
    <div id=”sub1″ class=”subMenu”>
    <a href=”#” onclick=”alert(‘sub11’)”>Sub 11</a>
    <a href=”#” onclick=”alert(‘sub12’)”>Sub 12</a>
    <a href=”#” onclick=”alert(‘sub13’)”>Sub 13</a>
    </div>
    <div id=”sub2″ class=”subMenu”>
    <a href=”#” onclick=”alert(‘sub21’)”>Sub 21</a>
    <a href=”#” onclick=”alert(‘sub22’)”>Sub 22</a>
    <a href=”#” onclick=”alert(‘sub23’)”>Sub 23</a>
    </div>
    <div id=”sub3″ class=”subMenu”>
    <a href=”#” onclick=”alert(‘sub31’)”>Sub 31</a>
    <a href=”#” onclick=”alert(‘sub32’)”>Sub 32</a>
    <a href=”#” onclick=”alert(‘sub33’)”>Sub 33</a>
    </div>
    </div>
    </body>
    </html>
    [/code]

    to post a comment
    JavaScript

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @voidvectorJan 28.2009 — when you attach an event to an element, the event would trigger even when it is for the child elements of the registered element.

    So when the mouse exits an anchor or a span element, it would still trigger, so you need to pass the event object into the hide function and check for srcElement/target to make sure it is actually the element you want.

    [code=html]
    <html>
    <head>
    <title>Simple Menu</title>
    <style type="text/css">
    .mainMenu { width:150px; }
    .subMenu { display:none; }
    </style>
    <script type="text/javascript">
    var SubMenus = ['sub1','sub2','sub3'];
    function hide(e) {
    if (e)
    {
    var srcElement = e.target || e.srcElement;
    if (srcElement.id != 'topMenu') return;
    }

    for (var i=0; i<SubMenus.length; i++) { document.getElementById(SubMenus[i]).style.display = 'none'; }
    }
    function show(IDS) {
    hide(); // test to remove display
    document.getElementById(IDS).style.display = 'block';
    }
    </script>
    </head>
    <body>
    <div id="topMenu" onmouseout="hide(event)">
    <a href="#" onmouseover="show('sub1')"><span class="mainMenu">Top 1</span></a>
    <a href="#" onmouseover="show('sub2')"><span class="mainMenu">Top 2</span></a>
    <a href="#" onmouseover="show('sub3')"><span class="mainMenu">Top 3</span></a>
    <div id="sub1" class="subMenu">
    <a href="#" onclick="alert('sub11')">Sub 11</a>
    <a href="#" onclick="alert('sub12')">Sub 12</a>
    <a href="#" onclick="alert('sub13')">Sub 13</a>
    </div>
    <div id="sub2" class="subMenu">
    <a href="#" onclick="alert('sub21')">Sub 21</a>
    <a href="#" onclick="alert('sub22')">Sub 22</a>
    <a href="#" onclick="alert('sub23')">Sub 23</a>
    </div>
    <div id="sub3" class="subMenu">
    <a href="#" onclick="alert('sub31')">Sub 31</a>
    <a href="#" onclick="alert('sub32')">Sub 32</a>
    <a href="#" onclick="alert('sub33')">Sub 33</a>
    </div>
    </div>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @vwphillipsJan 28.2009 — [CODE]<html>
    <head>
    <title>Simple Menu</title>
    <style type="text/css">
    #topMenu {
    width:460px;
    }

    .mainMenu { width:150px;border:solid black 1px;float:left }
    .subMenu { display:none; }
    </style>
    <script type="text/javascript">
    var SubMenus = ['sub1','sub2','sub3'];
    function hide(p) {
    var ev=window.event||arguments.callee.caller.arguments[0];
    if (zxcCkEventObj(ev,p)&&p.lst) p.lst.style.display='none';
    }
    function show(IDS) { // hide(); // test to remove display
    var sub=document.getElementById(IDS);
    if (sub.parentNode.lst) sub.parentNode.lst.style.display='none';
    sub.style.display = 'block';
    sub.parentNode.lst=sub;
    }
    function zxcCkEventObj(e,p){
    if (!e) var e=window.event;
    e.cancelBubble=true;
    if (e.stopPropagation) e.stopPropagation();
    if (e.target) eobj=e.target;
    else if (e.srcElement) eobj=e.srcElement;
    if (eobj.nodeType==3) eobj=eobj.parentNode;
    var eobj=(e.relatedTarget)?e.relatedTarget:(e.type=='mouseout')?e.toElement:e.fromElement;
    if (!eobj||eobj==p) return false;
    while (eobj.parentNode){
    if (eobj==p) return false;
    eobj=eobj.parentNode;
    }
    return true;
    }

    </script>
    </head>
    <body>
    <div id="topMenu" onmouseout="hide(this);" >
    <a href="#" onmouseover="show('sub1')"><div class="mainMenu">Top 1</div></a>
    <a href="#" onmouseover="show('sub2')"><div class="mainMenu">Top 2</div></a>
    <a href="#" onmouseover="show('sub3')"><div class="mainMenu">Top 3</div></a>
    <div id="sub1" class="subMenu">
    <a href="#" onclick="alert('sub11')">Sub 11</a>
    <a href="#" onclick="alert('sub12')">Sub 12</a>
    <a href="#" onclick="alert('sub13')">Sub 13</a>
    </div>
    <div id="sub2" class="subMenu">
    <a href="#" onclick="alert('sub21')">Sub 21</a>
    <a href="#" onclick="alert('sub22')">Sub 22</a>
    <a href="#" onclick="alert('sub23')">Sub 23</a>
    </div>
    <div id="sub3" class="subMenu">
    <a href="#" onclick="alert('sub31')">Sub 31</a>
    <a href="#" onclick="alert('sub32')">Sub 32</a>
    <a href="#" onclick="alert('sub33')">Sub 33</a>
    </div>
    </div>
    </body>
    </html>[/CODE]
    Copy linkTweet thisAlerts:
    @JMRKERauthorJan 28.2009 — Thank you both 'voidvector' and 'vwphillips' for helping to fill the void in my understanding!

    ?
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...