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 ?
Asked
Active
Viewed 1.7k times
17
-
7See this answer here. http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – Andrew Winter Jul 29 '11 at 09:35
2 Answers
41
window[name]()
You can call functions by name reference by selecting them as a property of window
and executing them

Adriano Carneiro
- 57,693
- 12
- 90
- 123

Raynos
- 166,823
- 56
- 351
- 396
1
have you tried variableName();
?
We often pass around callback functions, which might look something like this
function doSomething(callbackFunction){
// some happy code
callbackFunction()
}

Adriano Carneiro
- 57,693
- 12
- 90
- 123

Pwaddles
- 142
- 5
-
5Actually, the OP said the _name_ of the function is in the variable, not (a reference to) the function itself. – Ray Toal Jul 29 '11 at 09:40
-
this is what I was looking for - came up on Google for my search – Dave Hilditch Jul 18 '13 at 11:47