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?

5 Answers 5

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

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... =(
That's not a problem at all. tests[document.getElementById('myselect').value].foo()
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?

2 Comments

eval is not the solution to this problem
this is working however... If this is the only choice, then I'll give it a go. Thanks!
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.

2 Comments

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.
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.
0

Have you tried

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

Have a look at API/1.3/Selectors

3 Comments

I just have no problem with jQuery selectors. theArrayOfStrings[i] is not an id that we need to call... =(
So are you saying that theArrayOfStrings does not contain the test1, test2 etc that you wish to call? What does it contain then?
test1, test2 are not ids... they are just variable that keeps the value of the element resulting from jQuery plugin integration.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.