0

I have a set of defined variables. I dynamically set a value to another variable that ends up being the name of one of the other defined variables. I want to do this so I know when on the page to use which of the set variables.

var mwm = "blah"
var vp = "bleh"
var am = "bluh"

var mwm720 = "some link 1"
var mwm717 = "some link 2"
var vp720 = "some link 3"
var vp717 = "some link 4"
...

Then I have some other scripts that run and, depending on conditions, sets a variable value to be the name of one of the other variables.

var myClass = $(this).attr("class").split(' ')[1];
var currDiv = "div." + myClass;
var currVersion = $(this).parent().attr("id");
var linkVers = myClass + currVersion;

The linkVers variable value always ends up being "mwm720", "mwm717", "vp720", etc. That's how I know when to use the mwmw720 or mwm717 (etc) variables.

Desired behavior

How do I get an alert/call to use the value of the variable name that is stored in the dynamic variable?

Let's say alert(linkVers); returns "mwm720". How do I get $(currDiv).html(linkVers); to insert the value of the mwm720 variable, (which would be "some link 1"), and not the name of the variable?

If there's a better method for trying to accomplish this behavior, I'll certainly entertain other solutions.

4
  • 1
    You can get your variable value as window[linkVers] Commented Jan 18, 2013 at 18:27
  • Use dystroy's suggestion if the variables are global. If they are inside of a function, you will have to create an object where the keys are the names of your variables. Then you do the same thing: variables[linkVers]. Commented Jan 18, 2013 at 18:28
  • The dynamic variables are local, but the defined variables are global. Niels suggested an object below and it seems to be working. Commented Jan 18, 2013 at 18:35
  • Does this answer your question? "Variable" variables in JavaScript Commented May 12, 2022 at 20:25

4 Answers 4

5

You should put all your vars within 1 object. Like:

var messsages = {
    wm : "blah",
    p : "bleh",
    m : "bluh",

    wm720 : "some link 1",
    wm717 : "some link 2",
    p720 : "some link 3",
    p717 : "some link 4"
}

Then within your function you can use

alert(messages[linkVers]);
Sign up to request clarification or add additional context in comments.

2 Comments

Your syntax for the variables is broken (wrong placement of comma), but this seems to be working for what I need. I'll accept as an answer after some more testing. Thanks!
Yes this was psuedo code, untested, I've edited my message. Good luck!
0

It would be better to not crowd the window namespace with a bunch of variables. Use an object.

var myProps = {
    mwm720 : "some link 1",
    mwm717 : "some link 2",
    vp720 : "some link 3",
    vp717 : "some link 4"
};

than you can do

var foo = "mwm720";
console.log(myProps[foo]);

if you really want to do it the way you have written it, than use window with bracket notation.

var foo = "mwm720";
console.log(window[foo]);

Comments

0

I think what you're looking for is eval:

var myvar = "mvm720";
var mvm720 = "some link 1";

console.log(myvar);  // prints mvm720
console.log(eval(myvar));  //prints "some link"

So in your particular case:

eval ( $(currDiv).html(linkVers) ); 

Is what you're looking for

3 Comments

eval is not a good solution, just opens the page up to XXS holes.
@epascarello but it does answer his question doesn't it ? How do I get an alert/call to use the value of the variable name that is stored in the dynamic variable?
I did not say it did not answer it, I said it is not a good solution because of the security problems.
0

An even more flexible approach would be to group your variables by version. That way, you don't even have to worry about concatenating the name with the version.

var messsages = {
    720: {
        wm: "some link 1",
        p: "some link 3",
    },
    717: {
        wm: "some link 2",
        p: "some link 4",
    }
};

You can then get the correct version of the link text with:

var linkVers = messages[currVersion][myClass];

Since the currVersion won't change throughout the script's lifetime (as you're staying on the same page), you could also cache the messages of the current version at the start. That way, you don't need to constantly look up the parent's id:

// In some high scope
var currMessages;

// At startup (e.g. DOM ready)
// You probably need to slightly adjust this
// depending on what $(this).parent() actually signifies
var currVersion = $(this).parent().attr("id");
currMessages = messages[currVersion];

// When retrieving the link text
var linkVers = currMessages[myClass];

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.