Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dynamic variable name how to

    I am trying to concatenate text to refer to a variable:
    Code:
    var HomePlayer1Points = 17
    var i = 1
    var a = "HomePlayer"
    var b = "Points"
    var c = a + i + b
    
    alert(c.value)
    it give me undefined instead of 17. How should this be done without using eval() ???

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    var HomePlayer1Points = 17;
    var a = "HomePlayer";
    var b = " Points";
    var c = a + HomePlayer1Points + b;
    
    alert(c);  // NOT c.value
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i tried alert(c) but it gives me "HomePlayer1Points"

    what I want to get is the contents of HomePlayer1Points which is 17

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Code:
    <script type="text/javascript">
    var HomePlayer1Points = 17
    var i = 1
    var a = "HomePlayer"
    var b = "Points";
    var c=a+i+b;
    alert(window[c])
    </script>
    But only if the variable HomePlayer1Points is a global variable (as it belongs to the Global Object window). Is this what you want?
    Last edited by Kor; 11-10-2010 at 07:30 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well...
    considering that there are no functions being used it is global

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by larsof54 View Post
    i tried alert(c) but it gives me "HomePlayer1Points"

    what I want to get is the contents of HomePlayer1Points which is 17
    The code I gave you works perfectly. It yields HomePlayer 17 points
    Is that not what you want? You must have put HomePlayer1Points in quotes, making it a literal.

    That is one reason why it is good practice to name your variables with fairly short but recognisable names.
    Example: HP1Pts.

    Or if you simply want the value 17 to show, then it is as simple as

    Code:
    var HomePlayer1Points = 17;
    alert(HomePlayer1Points);
    Last edited by Philip M; 11-10-2010 at 07:38 PM.

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    All the Globals (including the Global set functions) are properties of the window object, and they can be treated as such. Here's a more eloquent version:
    Code:
    <script type="text/javascript">
    var HomePlayer1Points = 17
    var i = 1
    var a = "HomePlayer"
    var b = "Points";
    alert(window[a+i+b])
    </script>
    Last edited by Kor; 11-10-2010 at 07:34 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Or even clearer:
    Code:
    <script type="text/javascript">
    var HomePlayer1Points = 17; //global variable
    function test(){
    var i = 1; // local
    var a = "HomePlayer"; //local
    var b = "Points"; //local
    alert(window[a+i+b]);
    }
    test()
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    alert(window[c])
    So how is that better or simpler than

    Code:
    var HomePlayer1Points = 17;
    alert(HomePlayer1Points);
    Last edited by Philip M; 11-10-2010 at 07:44 PM.

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Philip M View Post
    So how is that better or simpler than

    Code:
    var HomePlayer1Points = 17;
    alert(HomePlayer1Points);
    It's a simple matter of finding/creating/destroying dynamically a global something. A it is, the OP's example is a pure academical one. But it becomes interesting when you want to create or handle on-the-fly that object/function:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    onload=function(){
    window['test']=function(){alert('foo')}
    }
    </script>
    </head>
    <body>
    <div onclick="test()">click me</div>
    </body>
    </html>
    It's just a matter of dynamical creating/finding/handling a Global, which is useful in many applications.
    Last edited by Kor; 11-10-2010 at 08:01 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Kor or your solution. That is exactly what I need to create dynamic references to variables. I have another similar question however:

    How can I reference form elements the same way?

    For example I have an input text field element by the name of HP1R1points. I would like to construct the reference to it the same way as the original question, ie. by concatenating strings and numbers.

    I tried alert(window["HP" + 1 " "R" + 1 + "points"]) but it results in a Javascript message of: [object HTMLInputElement]

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Code:
    document.forms[0]["HP"+1+"R" + 1 + "points"]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Oct 2010
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still not working:

    Code:
    alert(document.forms[0]["HP" + 1 + "R" + 1 + "points"])
    results in: [object HTMLInputElement]

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Yes??? That's what it *SHOULD* be.

    A *REFERENCE* to an *OBJECT*.

    What do you want to *DO* with it???

    Code:
    var formField = document.forms[0]["HP" + 1 + "R" + 1 + "points"];
    var value = formField.value;  // you want to get its value?
    if ( isNaN( parseInt( value ) ) 
    {
        formField.style.backgroundColor = "pink"; // you want to change its style?
        formField.value = "Try again";  // change its value?
    }
    ... and so on...
    How can I guess what you wanted to do with it after you got the reference?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Note that window["HP" + 1 " "R" + 1 + "points"] may or may not get you a FORM FIELD. Probably won't in any browser except MSIE 8 and below.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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