/    Sign up×
Community /Pin to ProfileBookmark

JavaScript dropdown menu issue

I’m trying to make a dropdown menu in JS, and I have some difficulties with it.
This is my code:

[CODE]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<title>Untitled Document</title>
<style>
@import “stil.css”;
</style>
<script type=”text/javascript” src=”test.js”></script>
</head>
<ul id=”sddm”>
<li><a href=”#” id=”home” onmouseover=”pokazi(‘m1’)” onmouseout=”tzatvori()”
>Home</a>
<div id=”m1″ >
<a href=”#” onmouseover=”pokazi2(‘m3’)” onmouseout=”tzatvori()”>HTML Drop Down</a>
<div id=”m3″>
<a href=”#” onmouseover=”pokazi(‘m3’)” onmouseout=”tzatvori()”>DHTML Menu</a>
<a href=”#” onmouseover=”pokazi(‘m3’)” onmouseout=”tzatvori()”>DHTML Menu</a>
<a href=”#” onmouseover=”pokazi(‘m3’)” onmouseout=”tzatvori()”>DHTML Menu</a>
<a href=”#” onmouseover=”pokazi(‘m3’)” onmouseout=”tzatvori()”>DHTML Menu</a>
</div>
<a href=”#” onmouseover=”pokazi(‘m1’)” onmouseout=”tzatvori()”>DHTML Menu</a>
<a href=”#” onmouseover=”pokazi(‘m1’)” onmouseout=”tzatvori()”>JavaScript DropDown</a>
<a href=”#” onmouseover=”pokazi(‘m1’)” onmouseout=”tzatvori()”>Cascading Menu</a>
<a href=”#” onmouseover=”pokazi(‘m1’)” onmouseout=”tzatvori()”>CSS Horizontal Menu</a>
</div>
</li>
<li><a href=”#” id=”home” onmouseover=”pokazi(‘m2’)” onmouseout=”tzatvori()”
>Gira</a>
<div id=”m2″ >
<a href=”#” onmouseover=”pokazi(‘m2’)” onmouseout=”tzatvori()”>HTML Drop Down</a>
<a href=”#” onmouseover=”pokazi(‘m2’)” onmouseout=”tzatvori()”>DHTML Menu</a>
<a href=”#” onmouseover=”pokazi(‘m2’)” onmouseout=”tzatvori()”>JavaScript DropDown</a>
<a href=”#” onmouseover=”pokazi(‘m2’)” onmouseout=”tzatvori()”>Cascading Menu</a>
<a href=”#” onmouseover=”pokazi(‘m2’)” onmouseout=”tzatvori()”>CSS Horizontal Menu</a>
</div>
</li>
</ul>
<div style=”clear:both”></div>
</html>
[/CODE]

[CODE]

*{
float:none;
margin:0;
padding:0;
}

#sddm li
{ float:left;
list-style:none;
}
#m1 {
visibility:hidden;
position:absolute;
}
#home{
display:inline;
}
#m2 {
visibility:hidden;
position:absolute;
}
#stuff a{
float:left;
display:block;
}
#sddm div a{
display:block;
}
#m3{
position:absolute;
visibility:hidden;
left:140px;
top:-5px;
}
[/CODE]

[CODE]
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function pokazi(id){
cleartimer();
if(ddmenuitem) ddmenuitem.style.visibility = ‘hidden’;
/*OVO MORA OVDE INACE NECE DA ZATVORI */
ddmenuitem=document.getElementById(id);
ddmenuitem.style.visibility=’visible’;
}
function pokazi2(id){
cleartimer();

ddmenuitem=document.getElementById(id);
ddmenuitem.style.visibility=’visible’;
}

function zatvori(){
ddmenuitem.style.visibility=”hidden”;
}
function tzatvori(){
closetimer=setTimeout(“zatvori()”,timeout);
}
function cleartimer(){
clearTimeout(closetimer);
closetimer=null;
}
[/CODE]

I’m still new with JavaScript so I’m having some problems here.
If you put your mouse over Home the dropdown menu will appear, and if you put your mouse over the first link in the dropdown (HTML Drop Down), you’ll see that the sidemenu appears.
Now comes the bad part.
When the sidemenu appears and you move your mouse to the second top link (Gira), you’ll see that sidemenu disappears but the previous menu stays and the new dropdown from Gira overlaps with the dropdown menu from Home.
Can someone help me with this one a bit?
I edited script from [URL=”http://javascript-array.com/scripts/simple_drop_down_menu”]http://javascript-array.com/scripts/simple_drop_down_menu[/URL] for this menu, because it seemed like the easiest solution for a JavaScript beginer.
If you have any easier way to make dropdown menus with JS feel free to give me a link.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SockwaveJan 10.2011 — I think the problem is with pokazi2, which wasn't in the original script. The function tzatvori only hides the last drop-down menu drawn. When you draw the sub-menu (m3) with your pokazi2 function, you reset ddemnuitem to be the m3 div. You continue resetting it by calling pokazi('m3') on mouseOvers. This means that the m1 div will never be hidden once the m3 div is shown, which I think is what you're describing. I would recommend the following: change pokazi2 to something like this:
<i>
</i>function pokazi2(id){
cleartimer();
var childMenuItem = document.getElementById(id); //don't reset ddmenuitem
childMenuItem.style.visibility='visible';
}

And then ALWAYS call pokazi2('m3') for the m3 div, even on the mouseOver events inside the menu. I think that should work.

Alternatively, rewrite tzatvori to take an id and only hide the correct elements.

As for the position of the second menu, you just have to give a "left" value in the css to m2.
×

Success!

Help @Mentalhead 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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