SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    san jsoe
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable variable in JavaScript

    Hi,

    Can I do variable variable in JavaScript? I 'm testing but so far no luck.

    SubMenu1 = "1.1 1.2 1.3";
    SubMenu2 = "2.1 2.2 2.3";

    currentSubMenu = "SubMenu" + 1;

    alert(currentSubMenu); /* Would alert string "SubMenu1"*/



    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Southampton, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use:
    HTML Code:
      <script type="text/javascript">
      SubMenu1 = "1.1 1.2 1.3";
      SubMenu2 = "2.1 2.2 2.3";
      
      currentSubMenu = eval("SubMenu" + 1);
      
      alert(currentSubMenu); /* Would alert string "1.1 1.2 1.3"*/
      </script>
    eval() will take the input string and evaluate it as if it were a variable.
    Good Luck,
    AjC

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AjC is right - but you don't have to use eval - in fact, don't use eval unless there's no other way to accomplish what you want... speaking of which, tell us what you're trying to do and we'll make suggestions

    edit... ok, here's another example:

    Code:
      var i = 1, ele;
      do {
        ele = document.getElementById('subMenu' + i);
        if (ele) {
          //...
          ++i;
        }
      } while (ele);
    edit... ok I admit - that example does not illustrate a "variable variable"

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Durham
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. It would likely be better to hold the info in an array rather than using eval() and a bunch of variables...

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Southampton, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Out of interest, what is wrong with eval()?
    I've had little experience with it, but was unaware it should be avaoided.

    Thanks,
    AjC

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var SubMenu1 = "1.1 1.2 1.3";
    var SubMenu2 = "2.1 2.2 2.3";
    
    currentSubMenu = window["SubMenu" + 1];
    
    alert(currentSubMenu); /* Would alert string "SubMenu1"*/
    Read this. Avoid eval() - slow, messy, risky (executes code strings automatically).
    ::: certified wild guess :::

  7. #7
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The simplest reason is just that it is a very inefficient way of accomplishing something. There has only been a few times when I could find no other way other than using eval.

    edit... ah, adios beat me to the punch


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •