1

I have a dynamically created variable called item_908 (where 908 is dynamically). Later on, there is a variable called id, which holds the string "908".

Now, i need to access the variable item_908 programmatically and i cannot remember how to do it. Can anyone tell me how?

1
  • 1
    Just don't do dynamic variables. Use a friggin' array. Or if it's more than a number, an object. But don't pollute scopes. Commented Aug 9, 2012 at 9:56

2 Answers 2

2

If they are in the global scope:

window["item_" + id];

else you can only access them throu an Object:

var obj = {},
    id = 908;

obj.item_908 = "abc";


obj["item_" + id]; // abc
Sign up to request clarification or add additional context in comments.

Comments

1

You may access it by using:

window['item_908'];

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.