tags:

views:

197

answers:

3

Hi

Please have a look on the following code -

var abc_text = "Hello";
var def_text = "world";

function testMe(elem) {
    var xyz = elem+"_text";
    alert(xyz);
}

testMe("abc");
testMe("def");

I am trying to pass prefix to a function and the try to print some pre-defined values by concatenating. But the above example just prints "abc_text" and "def_text" .. instead of "Hello" and "world". How can I get it working?

Thank you.

EDIT

I am using Jquery.

+5  A: 

in this case use

var xyz = window[elem+"_text"];
Jonathan Fingland
This (or alternately eval) is right. But wbdvlpr, note that it's rarely necessary to do this in well-written code.
Matthew Flaschen
eval is evil. eval should only ever be used in the rarest of situations. It is far too easy to get code injected from other sources.
Jonathan Fingland
@Jonathan: uh... well, no. It's only easy if you're accepting input from untrusted sources and then blindly passing it to eval(). But it's slow and in this case unnecessary; surely that's reason enough!
Shog9
@Jonathan Fingland - yes, it works. But associative array seems better to me. Thanks.
Wbdvlpr
@wbdvlpr, oh I agree. the question as asked is solved by the window[] method, but the associative array method is better practice overall
Jonathan Fingland
@Matthew, @Shog9: From Mozilla: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval#Don%27t_use_eval! , quote: "Don't use eval!". Also see: http://adblockplus.org/blog/five-wrong-reasons-to-use-eval-in-an-extension . for well-intentioned, but really bad advice see http://24ways.org/2005/dont-be-eval . The MDC page makes it clearest : don't use eval if the code *could* be affected by a malicious 3rd party. This doesn't just include user input. this also includes the inclusion of 3rd party scripts on your page, xhr responses that aren't very well sanitized, etc.
Jonathan Fingland
@Jonathan: yeah, yeah... I know all about eval's bad rep. Point is, there's no code injection risk in the example being discussed here. I hate seeing fear-mongering used in place of facts; if you don't want to go off on a tangent, then just say "use of eval() is generally discouraged, and is not required for this task" and leave it at that. BTW: if you're including untrusted 3rd-party scripts on your page, then you're already doomed - they can call eval() or any other function...
Shog9
@shog9. totally agree on that last point 3rd party scripts are generally a bad plan. Obviously in some cases, e.g. google maps, it's really unavoidable. My point, however, was that eval should be your last resort and most certainly not your first. you can call that fear mongering, or you can call it pragmatism.
Jonathan Fingland
+3  A: 
erenon
just to make erenon's point clearer. it would be accessed using, for example, var xyz = text[elem];
Jonathan Fingland
Thanks @erenon, and Thanks, again, Jonathan Fingland, for its usage code.
Wbdvlpr
+1  A: 

There is a pretty good write-up on Dynamic Variables in JavaScript here:

http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript

Eli