Passing JS variable name to function

So here's what I am trying to do:

  • My variable named is data_1

    set.

    var data_1 = {
      "y_legend": {
        "text": "# of Patients",
        "style": "{font-size: 20px; color: #778877}"
      },
      "x_legend": {
        "text": "AUG 09 - OCT 09",
        "style": "{font-size: 20px; color: #778877}"
      }
    };
    
          

  • In the dropdown list, the user selects the parameter with a value data_1

    , which is called load('data_1')

    .

    function load(data)
    {
      tmp = findSWF("my_chart");
      x = tmp.load( JSON.stringify(data) );
    }
    
          

My problem: I am selecting a parameter with a value data_1

, not the variable itself. So in my function load('data_1')

, when I alert(data)

, I get data = 'data_1'

.

So how do I get the content of my variable data_1

in my load function by passing only the string name?

+2


source to share


3 answers


var data_1 = { /* data goes here */ };

var data_choices = {1: data_1, 2: data_2, /* and so on */};

var load = function (data) {
    // data is "1", "2", etc. If you want to use the full data_1 name, change
    // the data_choices object keys.

    var tmp = findSWF("my_chart");
    var x = tmp.load( JSON.stringify(data_choices[data]) );
}

      



+3


source


If it is a global variable, you can refer to it with

window['the_variable_name']

      



eg.

function load(data)
{ 
  tmp = findSWF( "my_chart" ); 
  x = tmp.load( JSON.stringify( window[data] ) ); 
}

      

+3


source


or you can just use

alert(eval(data)) 

      

+2


source







All Articles