0

Below is an example of my code

/// Control vars ///

/// Control var ID ///
var id=2;
///

/// Load Fields by ID ///
var name_s_+id=jQuery("#name_c_"+id).val();
///


alert("name_s_"+id);

I have an ID which can be, for example, any value in (1,4,5,3,2 ....... etc)

I would like to create references using the naming scheme var name_s_(Load ID)

How does JavaScript support such dynamic variable names, and how can I reference the value in my code to, for example, alert it's value:

alert("name_s_"+id);
nbrooks
  • 18,126
  • 5
  • 54
  • 66
user2501504
  • 311
  • 3
  • 7
  • 15
  • possible duplicate of [Programmatically setting the name of a variable](http://stackoverflow.com/questions/8525221/programmatically-setting-the-name-of-a-variable) – nbrooks Jul 24 '13 at 05:48

1 Answers1

1

Use an object.

var id=2;

var name_s = {};

name_s[id] = jQuery("#name_c_" + id).val();

alert(name_s[id]);

If your IDs are fairly consecutive and start at 0 (or even 1), you can use an array instead:

var name_s = [];

(and the rest is the same).

Amadan
  • 191,408
  • 23
  • 240
  • 301