tags:

views:

192

answers:

7

Say I have the following JS:

var foo_index = 123;
var bar_index = 456;

And the following HTML:

<div id="foo"></div>
<div id="bar"></div>

Then I'd like to say this:

thisIndex = this.id + '_index'

And I'd like thisIndex to be a number. How do I turn the string, which is exactly the variable name, into a variable?

+11  A: 

You should put the variables in an object, like this:

var indices = { 
    foo: 123,
    bar: 456
};

var thisIndex = indices[this.id];

This code uses JSON syntax an object literal to define an object with two properties and uses [] to access a property by name.

You can also write

var indices = new Object;
indices.foo = 123;
indices["bar"] = 456;
SLaks
You got it completely backwards. It's JSON that uses a subset of the Javascript syntax. This is a Javascript object, and it has nothing at all to do with JSON.
Guffa
@Guffa: I realize that. However, the syntax is usually referred to as JSON syntax. What would you call it?
SLaks
@SLaks: It's an object literal. The syntax has been around long before JSON existed, so it's only very recently that people have started to (incorrectly) refer to it as JSON syntax.
Guffa
Just to clarify: JSON is simply a textual representation of a JS object. It's a serialized form of an object.
reko_t
Doesn't JSON just mean JavaScript Object Notation? 'JSON syntax' is kind of a redundant acronym, but since object notation is exactly what you used in the declaration you were right to refer to it as JSON in the first place.
Keith
@Keith: No, it's not right. It's **not** JSON. In fact, the code that SLaks showed doesn't even work if you try to use it as JSON. The JSON syntax is a subset of the Javascript syntax, and the code for this object literal is outside of that subset.
Guffa
_Bows to pressure_...
SLaks
+5  A: 

you can try using the eval function:

http://www.w3schools.com/jsref/jsref_eval.asp

it does exactly what you need.

tarrasch
But very, _very_, slowly.
SLaks
If you're thinking of using *eval()*, chances are there's a better way of doing what you're trying to do (in this case, SLaks' answer). [Eval is Evil](http://blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx), by Eric Lippert is a good read on the subject.
Andy E
+1  A: 

To answer your question, you can use the eval function to evaluate a string:

thisIndex = eval(this.id + '_index');

However, using the eval function is generally a sign of badly constructed code. I think that you should use an associative array instead:

var numbers = { foo: 123, bar: 456 };
thisIndex = numbers[this.id];
Guffa
rather, use JSON.parse it's a lot safer than eval. download json2.js on google.
Martin Ongtangco
@Martin: *JSON.parse()* wouldn't help here, it will only parse a JSON string, which isn't the intent.
Andy E
+10  A: 

You can. If foo_index and bar_index are global variables, you can simply do:

var thisIndex = window[this.id + '_index'];
reko_t
That's quite helpful to know. Thanks.
Isaac Lubow
+1 This should have been the right answer since it doesn't require the asker to change his code.
Juan Mendes
While it was helpful to know, my desire was to change the code.
Isaac Lubow
+1  A: 
window["myvar"] = 'hello';

alert(myvar);
PAP
+1  A: 

I am not sure what do you want to achieve, but maybe this approach could be better (it depends on some factors like version of HTML you use as @Andy E points in comment below):

<div id="foo" index="123"></div>
<div id="bar" index="456"></div>
<script>
   var fooIndex = document.getElementById("foo").getAttribute("index");
</script>

Here value of index is kept together with corresponding HTML element.

Grzegorz Gierlik
Custom attributes are not allowed in HTML4 and will invalidate your markup. However, it would be valid HTML5 if you prefix the attribute names with *data-*, e.g. `data-index="123"`.
Andy E
This is why I wrote "would be better". "could" would be better.
Grzegorz Gierlik
A: 

I think you want something like this:

// put your properties in an object of some kind
var dictionary = 
{
    foo_index: 123,
    bar_index: 456
};

// you can set further properties with property syntax
dictionary.again_index = 789;

// or dictionary syntax - same result
dictionary['another_index'] = 012;

// then function to get the number value from the index name becomes
var thisIndex = Number(dictionary[this.id + '_index']);
Keith
Why are you calling `Number`?
SLaks
@SLaks - yeah, I don't really need to, just being explicit as the OP said that they wanted a number back.
Keith