title: How to create a dynamic var in Javascript
body: <p>I use the following in perl to create dynamic variables <code>$$x = "color";</code> This will allow me to access the variable as $color. I want to do something similar in JavaScript. I want to create dynamic variable names in JavaScript that come from an unknown HTML FORM of key value pairs. In other words lets say I read in name/value pairs looping through document.forms['1'] with values of pagetitle=fishing, phone=555-5555, and color=red. I want create JavaScript variable names of pagetitle, phone, and color so I can perform in-place html updates, following a successful db update, using something like so </p>
  
  <pre><code>var f = document.forms[1];
  for ( var i = 0; i &lt; form.elements.length; i++ ) {
        var e = f.elements[i];
        e.name = e.value);   /* something like $$e.name = e.value in PERL */ 
  }
  
  document.getElementById('pagetitle').innerHTML = pagetitle;
  document.getElementById('phone').innerHTML = phone;
  document.getElementById('field1').style.background = color;
  document.getElementById('field2').style.background = color;
  </code></pre>
  
  <p>It's the e.name and e.value area that I don't know how to make into a dynamic variable?</p>
  
  <p>I know I could just do something like so...</p>
  
  <pre><code>document.getElementById('pagetitle').innerHTML = document.forms[1]['pagetitle'].value;
  </code></pre>
  
  <p>but I would still like to know how to work with dynamic variables in JavaScript.</p>