views:

67

answers:

5
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?

A: 

Have you tried

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

Have a look at API/1.3/Selectors

astander
I just have no problem with jQuery selectors. theArrayOfStrings[i] is not an id that we need to call... =(
Jronny
So are you saying that theArrayOfStrings does not contain the test1, test2 etc that you wish to call? What does it contain then?
astander
test1, test2 are not ids... they are just variable that keeps the value of the element resulting from jQuery plugin integration.
Jronny
+2  A: 

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?

Canavar
eval is not the solution to this problem
Fabien Ménager
did you read OP's comment at astander's answer ?
Canavar
this is working however... If this is the only choice, then I'll give it a go. Thanks!
Jronny
A: 
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
the test1ID, and test2ID should actually be hard-coded. =(
Jronny
+4  A: 

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();
}
J-P
Nice idea, but the problem is what if I do not use loop to access foo() instead, I use a <select> and with the variable names ("test1", "test2") as the val per option... =(
Jronny
That's not a problem at all. `tests[document.getElementById('myselect').value].foo()`
bobince
+1  A: 

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
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
the problem here could be that if test1 is not a global variable...
Jronny
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