Mack
Mack

Reputation: 47

Specify onmouseleave function that has 1 parameter

I have a dropdown list in my navbar. I do something a little different to usual drop down lists, instead of having the drop down list created & just hidden, I create(when a link is hovered over) & destroy it(when you stop hovering over that link) dynamically using javascript.

My Problem: I am attempting to set onmouseleave on the dropdown list div(that I create in javascript). But the function that I want to be called on onmouseleave takes one parameter - how do I specify this parameter?

dropDownList.onmouseleave = onLeaveEx;  // how do I specify the 1st parameter? Such as onLeaveEx("aboutUsLink")  

    function onLeaveEx( linkName )
    {
        if ( navSubMenu != null )
        {
            navSubMenu.parentNode.removeChild( navSubMenu ); 
            navSubMenu = null;
        }
    }

Upvotes: 2

Views: 122

Answers (2)

hugomg
hugomg

Reputation: 69934

What you want is a closure. This allows to pass some parameters beforehand:

function make_callback(link_name){
    function actual_event_handler(evt){
        //this inner function can use link_name
        //here
    }
    return actual_event_handler;
}

dropDownList.onmouseleave = make_callback("a linke name");

Upvotes: 1

hayesgm
hayesgm

Reputation: 9096

Use an anonymous function:

dropDownList.onmouseleave = function() { onLeaveEx(my_parameter); };

Upvotes: 1

Related Questions