17

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 ?

user769154
  • 227
  • 1
  • 2
  • 8
  • 7
    See 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 Answers2

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