0
var test1;
$(document).ready(function () {
    test1 = $("#test1ID").jQueryPlugin();
});

var test2;
$(document).ready(function () {
    test2 = $("#test2ID").jQueryPlugin();
});

...

This is done so we could just do test1.foo()... foo is a function inside the jQueryPlugin that is accessible using test1.foo() syntax;

So we have an array of strings which contains (test1, test2, ...) and we need to access foo() while on the loop:

for(i=0; i < theArrayOfStrings.length; i++){
    theArrayOfStrings[i].foo();
    //so here is the problem... we can't do test1.foo(), test2.foo() ... =(
}

Any idea on how to call function foo() while on the loop? Or can we convert a string value to a variable in javascript?

Jronny
  • 2,164
  • 4
  • 30
  • 41

5 Answers5

4

It might be worth creating an object to hold all your "tests":

var tests = {};

$(document).ready(function () {
    tests.test1 = $("#test1ID").jQueryPlugin();
    tests.test2 = $("#test2ID").jQueryPlugin();
});

for(i=0; i < theArrayOfStrings.length; i++){
    tests[theArrayOfStrings[i]].foo();
}
James
  • 109,676
  • 31
  • 162
  • 175
  • Nice idea, but the problem is what if I do not use loop to access foo() instead, I use a – Jronny Dec 12 '09 at 13:49
  • That's not a problem at all. `tests[document.getElementById('myselect').value].foo()` – bobince Dec 12 '09 at 14:27
2

eval() function is used to evaluate script in a string variable. For example :

var test1;
eval("test1=" + theArrayOfStrings[i]);
test1.foo();

But take a lok at this question before use When is JavaScript’s eval() not evil?

Community
  • 1
  • 1
Canavar
  • 47,715
  • 17
  • 91
  • 122
1

If test1 is a global variable you can access it by name through the window object:

window[theArrayOfStrings[0]].foo();   // test1();

If it's not, eval is the only way, but I'd strongly advise avoiding eval in all circumstances. Using a lookup as in J-P's answer (+1) is much much more appropriate than selecting variable names.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Or more generally, you can use *this* instead of *window*, as: this['test1'](); // test1(); However, you still can only reference global variables, not local ones. – Eino Gourdin Dec 12 '09 at 13:34
  • Well, you can get the current function with arguments.callee. But you still couldn't access the function's local (inner) variables this way. So, you could declare the variables as the function's property, like: arguments.callee.test1 = ...; and then arguments.callee['test1'](). But this way comes back to J-P's proposition, which I think is the cleanest solution. – Eino Gourdin Dec 12 '09 at 14:00
0

Have you tried

$('#' + theArrayOfStrings[i]).foo();

Have a look at API/1.3/Selectors

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • I just have no problem with jQuery selectors. theArrayOfStrings[i] is not an id that we need to call... =( – Jronny Dec 12 '09 at 12:52
  • So are you saying that theArrayOfStrings does not contain the test1, test2 etc that you wish to call? What does it contain then? – Adriaan Stander Dec 12 '09 at 12:56
  • test1, test2 are not ids... they are just variable that keeps the value of the element resulting from jQuery plugin integration. – Jronny Dec 12 '09 at 13:36
0
var test = [], n = 5;
$(document).ready(function () {
    for(var i=0; i < n; i++)
        test.push($("#test"+i+"ID").jQueryPlugin());
});

// the values in test won't be accessible before the document is loaded.
Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60