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.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question String as a variable

    Please take a look at this function:
    Code:
    function test(p1, p2)
    {
    	"a" + p1 + "b" + p2 = 10;	// If p1 = 1, p2 = 2, variable a1b2 = 10
    }
    I want "a" + p1 + "b" + p2 to be considered as a variable. I tried eval("a" + p1 + "b" + p2) = 10, but that didn't work. How can I make a string to represent a variable name?

    Thanks.

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    if it's a global variable, then you want

    function test(p1, p2) {
    window["a" + p1 + "b" + p2] = 10; // If p1 = 1, p2 = 2, variable a1b2 = 10
    }

    hope that helps
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that helped.


  •  

    Posting Permissions

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