-3

Im not even sure how to word this and is probably why I am having trouble finding an answer in google.

When the code is run currentCardRow will equal 1 therefore it should be cardSelected1 which is what is shown in the console.log. I need it to go a step further because cardSelected1 is a variable and I need it to evaluate show in the console log as Invitation. Invitation is an example of a variable for cardSelected1.

I am not sure on what the correct syntax is to make this happen.

var currentCardSelected = "cardSelected" + currentCardRow;

Thanks for your help!

Craig Rinde
  • 95
  • 1
  • 2
  • 8
  • Why don't you just use an array? – Barmar Oct 25 '13 at 03:42
  • So `window.cardSelected1` will equal something? Is that what you're asking? – Qantas 94 Heavy Oct 25 '13 at 03:43
  • Your choice of words has confused your question. What does 'evaluated as a variable' mean? Your one-line code snippet isn't helping to clarify, either. Sorry - this is not at all clear, and will probably be closed on that basis unless you edit it. –  Oct 25 '13 at 03:43
  • If `cardSelected1` is a global variable you can use `window['cardSelected'+currentCardRow]`, but this isn't possible with local variables. – Barmar Oct 25 '13 at 03:43
  • Sorry, I am a novice so I don't have all of the terminology. I tried to explain to the best of my ability :). – Craig Rinde Oct 25 '13 at 03:47
  • object["cardSelected" + currentCardRow] where object is any object, including 'window' – aaronps Oct 25 '13 at 03:48
  • Like everyone else I'm wondering what the heck you're doing that you have a bunch of "cardSelected7" and "cardSelected25" just floating around in local scope? – jpsimons Oct 25 '13 at 03:48

2 Answers2

2

JavaScript has an Eval() function which allows you to evaluate strings as javascript code.

For example

var bar = "123";
var foo = "bar";
console.log(eval(foo));

will print "123" to the console.

For more information on eval, you can consult the MDN docs.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

Generally, the use of eval() is considered poor practice as it makes the code difficult to read. There are likely more elegant solutions to implement what you have described, however, eval will solve your current problem.

Code Monkey
  • 1,785
  • 11
  • 13
  • 2
    No, the contents of foo is a string containing the name of the variable bar. Foo will interpolate to bar, and hence eval will return "123"; – Code Monkey Oct 25 '13 at 03:47
  • `eval` is generally held to be bad practice: a) because it makes code harder to read and maintain, and b) because it can introduce security holes. –  Oct 25 '13 at 03:48
  • Indeed, I will amend my answer to warn the user. – Code Monkey Oct 25 '13 at 03:48
0
var currentCardSelected = eval("cardSelected" + currentCardRow);

This is how my problem is fixed.

Naveed Butt
  • 2,861
  • 6
  • 32
  • 55
Craig Rinde
  • 95
  • 1
  • 2
  • 8
  • I'm afraid that's an horrible solution. If you need this you have serious design issues in your code. Could you tell us why you need this? – plalx Oct 25 '13 at 03:58