Call javascript function which name is in variable

17,073

Solution 1

window[name]()

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

Solution 2

have you tried variableName(); ?

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

function doSomething(callbackFunction){
    // some happy code
    callbackFunction()
}
Share:
17,073

Related videos on Youtube

user769154
Author by

user769154

Updated on June 04, 2022

Comments

  • user769154
    user769154 almost 2 years

    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 ?

  • Ray Toal
    Ray Toal almost 13 years
    Actually, the OP said the name of the function is in the variable, not (a reference to) the function itself.
  • Dave Hilditch
    Dave Hilditch almost 11 years
    this is what I was looking for - came up on Google for my search