18

The problem is next: Assume that we have a select. Onchange event we need to call some function, but name of this function kept in variable. How to call this function ?

1

2 Answers 2

41
window[name]()

You can call functions by name reference by selecting them as a property of window and executing them

Sign up to request clarification or add additional context in comments.

Comments

1

have you tried variableName(); ?

We often pass around callback functions, which might look something like this

function doSomething(callbackFunction){
    // some happy code
    callbackFunction()
}

2 Comments

Actually, the OP said the name of the function is in the variable, not (a reference to) the function itself.
this is what I was looking for - came up on Google for my search

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.