How do you use value of variable as variable name?

4 replies
I have these lines:

var ddArray=['holidayTypes','groupType','group_status'];
for(var x in ddArray) eval(ddArray[x]=0);
alert(holidayTypes);
alert(groupTypes);
alert(group_status);
But it only alerts something like:

object HTMLSelectElement
Please help, thank you.
#variable
  • Profile picture of the author kdm5157
    Can you post what you're goal is? Maybe knowing that can point us in the right direction.
    {{ DiscussionBoard.errors[5815475].message }}
  • Profile picture of the author jassmee
    Banned
    [DELETED]
    {{ DiscussionBoard.errors[5954320].message }}
    • Profile picture of the author phpg
      It's much better to do something like this:

      Code:
      var ddArray=['holidayTypes','groupType','group_status'];
      for(var x in ddArray) window[ddArray[x]] = 0;
      {{ DiscussionBoard.errors[5954417].message }}
    • Profile picture of the author Earnie Boyd
      Originally Posted by jassmee View Post

      If you are interested to use value of variable as variable name
      then example below: -
      <?php
      =12;
      =$;
      ?>
      Except that the code given isn't PHP it is javascript and your example will fail since $12 isn't a valid variable name.
      Signature
      {{ DiscussionBoard.errors[5957624].message }}

Trending Topics