McB
McB

Reputation: 1232

How to use an argument from one function to call another function

I'm trying to figure out how to pass an argument to one function, which is actually a second function. Hard to explain so here is some sample code:

keyDownEvent("#thisID",nameOfFunctionToRunOnOpen);

function keyDownEvent(id, openFunction) {
$(id).dialog( {
    close: focusOnInput(),
    open: nameOfFunctionToRunOnOpen,
    modal: true,
    show: "clip",
    hide: "fade"
});
}

Everything else is standard for my .dialog() calls, just want to modify what happens on Open. Any idea how this can be achieved?

Upvotes: 1

Views: 149

Answers (3)

jwueller
jwueller

Reputation: 30991

You can simply pass the function to your second function. Functions are objects in JavaScript and can be saved in variables:

function outer(someOtherFunction) {
    someOtherFunction();
}

outer(function () {
    alert('inner function');
});

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

Just use an anonymous function:

function keyDownEvent(id, openFunction) {
$(id).dialog( {
    close: focusOnInput(),
    open: function(){openFunction(someArgument);},
    modal: true,
    show: "clip",
    hide: "fade"
});
}

Upvotes: 1

Jeremy Elbourn
Jeremy Elbourn

Reputation: 2700

Your second parameter should be given an actual function object and not just the name of the function in a string.

Upvotes: 0

Related Questions