-1

I have 3 jQuery functions with indexed progressive name such as:

function foo_1()
{
   ...
}

function foo_2()
{
   ...
}

function foo_3()
{
   ...
}

I need to dynamically discern the functions as shown:

foo_[i]

The overall purpose is to call a different function depending on the value of i variable.

How should I do that?

Garrett Kadillak
  • 952
  • 8
  • 18
Luca Detomi
  • 5,220
  • 7
  • 48
  • 73
  • possible duplicate of [How to execute a JavaScript function when I have its name as a string](http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string) – emerson.marini Sep 24 '14 at 13:15

2 Answers2

1

try this for example:

var i = "3";
window["foo_" + i]();
Alessandro Minoccheri
  • 34,369
  • 22
  • 118
  • 164
0

Another way is using the plain Javascript method eval():

var index = 1;
eval("foo_"+index+"()");
Reporter
  • 3,776
  • 5
  • 31
  • 46