/    Sign up×
Community /Pin to ProfileBookmark

passing parameters to onmouseover/out functions

Hi, I really need some help with this code. I’m trying to build a simple dropdown sub menu system, but I’m having troubles getting my code to work. Mainly, I want to pass a variable into a function called by the onmouseover and onmouseout events from an external javascript file. Here’s the code:

var totalDropMenus = 4;
var dropMenuOutTimer;
var navMenu;
var dropMenu;

window.onload = initDropMenus;

function initDropMenus() {
for (i=1; i < totalDropMenus; i++) {
navMenu = document.getElementById(“navMenu” + i);
dropMenu = document.getElementById(“dropMenu” + i);
[COLOR=”Red”]navMenu.onmouseover = showDropMenu(i);//when I pass [I]i[/I] into the function, the function does not work at all.
navMenu.onmouseout = hideDropMenu(i);[/COLOR]

}
}

function showDropMenu(index) {
button = document.getElementById(“navMenu” + index);
menu = document.getElementById(“dropMenu” + index);
menu.style.visibility = “visible”;
xPos = button.offsetLeft;
menu.style.left = xPos + “px”;
}

function hideDropMenu(index) {
menu = document.getElementById(“dropMenu” + index);
menu.style.visibility = “hidden”;
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineMar 25.2009 — The value of [I]i[/I] has to be saved:
[CODE]navMenu.onmouseover = ( function(n){ return function(){showDropMenu(n);} } )( i );
[/CODE]
Copy linkTweet thisAlerts:
@existingauthorMar 25.2009 — Sterling Isfine: You are GENIUS! That works perfectly!!! Thanks so much!

The only thing is that I don't understand the code you gave me 100%. How does the '(i)' at the end work? What exactly is happening?
Copy linkTweet thisAlerts:
@toicontienMar 25.2009 — It's creating a function closure. Function closures are kind of complex.
[CODE]function outer(x) {
var inner = function () {
return x + 1;
};

return inner;
}[/CODE]


In the example above, the [B]x[/B] variable is an argument passed to the [B]outer[/B] function. The outer function returns a function object called [B]inner[/B]. Since the function called inner was declared inside the function called outer, the inner function has access to the x variable even after the outer function has finished executing.

[CODE]var a = outer(0);
var b = outer(2);

alert( a() ); // Alerts 1: 0 + 1
alert( b() ); // Alerts 3: 2 + 1[/CODE]
Copy linkTweet thisAlerts:
@existingauthorMar 26.2009 — Thanks all for your help!
×

Success!

Help @existing 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 5.14,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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