4

Ok you have a set of variables:

var height1 = 10
var height2 = 20
...
var height7 = 70;

The user has given input and you need to get the value of one of those variables, here's the code for that:

if(window.location.hash) {
      var hash = window.location.hash.substring(1); 
            var option_name = $('a[name=' + hash + ']').closest('[id^="option"]').attr("id");
            var hash_div_height_id = "height" + option_name.substring(6);
            $('a[name=' + hash + ']').closest('[id^="option"]').show();
            $('a[name=' + hash + ']').closest('[id^="option"]').height(hash_div_height_id);
  } else {
      // No hash found
  }

My problem now being that I can't pass "hash_div_height_id"(which would currently represent the string "height1" or "height2" or whatever number the jQuery returned to append to the end of the string) to the height function as it's a string.

So how can I go about this? Can I somehow convert the string "height1" to represent the variable height1 that was established earlier?

javArc
  • 317
  • 2
  • 6
  • 14
  • 2
    I think you want [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators#Bracket_notation). I'll see if I can find a good duplicate. – apsillers Jun 18 '13 at 17:22
  • I'd say use `eval`, but i know that i'd be castrated ;) – krishwader Jun 18 '13 at 17:24
  • Duplicate of [Access value of JavaScript variable by name?](http://stackoverflow.com/questions/4399794/access-value-of-javascript-variable-by-name) and [Javascript dynamic variable name](http://stackoverflow.com/questions/5117127/javascript-dynamic-variable-name) – apsillers Jun 18 '13 at 17:24
  • Start by not having a "set of variables". Objects exist for a reason. – hobbs Jun 18 '13 at 17:26
  • Whenever you have variables of the form `name#`, then you want to consolidate the values into an array or object. – Felix Kling Jun 18 '13 at 17:28
  • Possible duplicate of http://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript – Khawer Zeshan Jun 18 '13 at 17:29
  • If you can [GOOGLE](https://www.google.com/search?q=string+name+variable+javascript&rlz=1C1GGGE_enPK422PK445&oq=string+name+as+variable+ja&aqs=chrome.1.57j0l3j60.14103j0&sourceid=chrome&ie=UTF-8) you will get the answer – Khawer Zeshan Jun 18 '13 at 17:31
  • @apsillers thanks, I found that thread when I was researching initially but I was having trouble getting it to work. Though I've learned alot about 'window' today. – javArc Jun 18 '13 at 18:47
  • I had actually found the thread you mentioned in your comment when I was researching @Khawer but I couldn't get either the window or eval to work the way I needed it to. That and I didn't want to use either unless absolutely necessary. I figured my question had enough nuance in it to warrant a separate thread. – javArc Jun 18 '13 at 18:48

3 Answers3

5

Assign the height's as properties of an object.

Something like..

var heightObj = {
        height1 : 10,
        height2 : 20,
        height7 : 70
    };

var hash_div_height_id = "height" + option_name.substring(6);

To access the particular property , use the [] notation to access the property

var newHeight = heightObj[hash_div_height_id];
$('a[name=' + hash + ']').closest('[id^="option"]').height(newHeight);
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
2

Assuming that the variables are declared in global scope you can just do window[hash]

That because any variable you declare in global scope is attached as a prpoerty to window (assuming the code is run in a browser). and you can get the value of any property either by dot notation

window.height1

or by indexing using the property name as the key. That is

window["height1"]

and since you can pass a variable holding the key you can simply do

var hash = "height1"; //replace with the code you already have
var height = window[hash];
Rune FS
  • 21,497
  • 7
  • 62
  • 96
0

Do you have control over the variables as an array of heights would suit you much better, failing that, create the array yourself so you take height 1..X push them onto an array and then you can just use the index.

Mocksy
  • 360
  • 2
  • 7