0

Easiest to explain by showing code:

var delta_selected = false;
var magma_selected = false;
var aqua_selected = false;

// Somewhere in my functions...
if (sumthing == 'sumthing1') {
    topic = 'delta';
} else if (sumthing == 'sumthing2') {
    topic = 'magma';
} else {
    topic = 'aqua';
}

// Then assign it (so if topic is delta, then delta_selected = true)
topic + "_selected" = true;

This last line isn't working. Syntax error.

I need help to figure out how to do this. Thanks!

2
  • 1
    Why not just set delta_seleted = true instead of topic = 'delta', etc... Commented Feb 23, 2012 at 19:16
  • If I could, I would. :D I only posted some of the code so you can't see why I cant just specify the name. Commented Feb 23, 2012 at 19:55

3 Answers 3

3

try this

window[topic + "_selected"] = true;
Sign up to request clarification or add additional context in comments.

Comments

3

Why use variables? Looks to me like you need a single structure that contains all your _selected values.

var selected = {
    delta:false,
    magma:false,
    aqua:false
};

then...

if (sumthing == 'sumthing1') {
    topic = 'delta';
} else if (sumthing == 'sumthing2') {
    topic = 'magma';
} else {
    topic = 'aqua';
}

selected[topic] = true;

Comments

0

Take a look at the "eval()" function. You will probably end up with something like:

eval(topic + "_selected = true;")

But check the documentation to be sure.

Hope this helps.

1 Comment

yes, I wouldn't want to use eval. Nice to know that I can do it like that though.

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.