-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
  • 1,026
  • 9
  • 18
Luca Detomi
  • 5,564
  • 7
  • 52
  • 77
  • 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
  • 35,521
  • 22
  • 122
  • 171
0

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

var index = 1;
eval("foo_"+index+"()");
Reporter
  • 3,897
  • 5
  • 33
  • 47