SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text strings to variable name?

    Is it possible to dynamically create a new variable name from existing strings?

    For example, if I had a strings:

    "var1" and "var2"

    Assuming no prior knowledge of the values of the strings, would it be possible to create a variable called 'var1var2' and assign a value to it?

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eval('var ' + var1 + var2 + ' = "";');
    ----Adopt-a-Sig----
    Your message here!

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but the only way I know to make this work is to have them be global...
    Code:
    var str1 = "var1";
    var str2 = "var2";
    window[str1+str2] = "Hello World!";
    alert(var1var2);
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Randem:

    No joy... I'm getting errors with your code as I did with my various experiments with Eval.

    Beetle:

    Assuming the last line is meant to be alert(str1str2); I get an error with this as well.

    Can either of you expand on these approaches, or has anyone else hot any ideas?

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jetboy
    Assuming the last line is meant to be alert(str1str2); I get an error with this as well.
    No! It should be just as I wrote it!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, man... instead of taking an attitude like we're giving you bad suggestions, why don't you tell us what errors you're getting.

    Both suggestions -- mine and beetle's -- are correct, and should work. I'm willing to bet your errors are coming from something completely unrelated to the advice you've been given.
    ----Adopt-a-Sig----
    Your message here!

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not trying to give 'attitude' Randem, and reading over my previous post I didn't think there was any. Apologies for any offence caused...

    This is what I've got:

    <html>
    <head></head>
    <body>
    <script language="JavaScript1.2">
    var var1 = 1;
    var var2 = 2;
    eval('var ' + var1 + var2 + ' = "";');
    document.write(var1);
    </script>
    </body>
    </html>

    which gives me:

    Line: 7
    Char: 1
    Error: Unexpected identifier

    in IE6.

    Commenting out Line 7 results in the script running without an error, and printing the value of var1 to the screen.

    I've made things as simple as I can to test the code; the final code needs to be used in a function with passed arguments and new string name fragments pulled from select box names using the DOM... to them be passed out to PHP as hidden fields on a form.

  8. #8
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you can't do that.

    var1 = 1
    var2 = 2

    what do you think 'var ' + var1 + var2 + ' = ";' is going to be?
    Code:
    var 12 = ";
    which is obviously not legal javascript syntax. variable names can't start with a number.

    and regarding attitude. I meant exactly what I said, which had nothing to do with offending or being offensive.
    ----Adopt-a-Sig----
    Your message here!

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, got it:

    I'm trying to operate on the variable names and you guys are operating on the string contents - hence no working code for me. Bear that in mind and my last two posts might make a bit more sense. Yes, all your code works fine, my mistake.

    Only problem is, like I mentioned, I don't have prior knowledge of the string contents, therefore can't refer to the actual strings, just the variables that will hold them. I probably didn't make this particularly clear in my first post. To try and put this in context:

    newvar1 = document.forms[formname].elements[elementname].name;
    newvar2 = document.forms[formname].elements[elementname].name;

    and I'm looking to create a new variable whose name is the two form element names combined.

    So if newvar1 ended up being "name1" and newvar2 ended up being "name2", I'd be after a new variable called name1name2. This code is in a function, and the values of newvar1 and newvar2 will be different every time it's called, hence no prior knowledge...

    Surely it's not as simple as dropping the DOM code into an eval() statement?

  10. #10
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jetboy...did you even test me example without changing it? It works!!! I assure you it works!!!
    To use your example....
    Code:
    <html>
    <head>
    <title>The unknown variable</title>
    
    <script language="javascript">
    function makeVar() {
    	var s1 = document.forms['testForm'].elements[0].name;
    	var s2 = document.forms['testForm'].elements[1].name;
    	window.myVar = window[s1+s2];
    	myVar = document.forms['testForm'].elements[0].value + document.forms['testForm'].elements[1].value;
    	}
    </script>
    </head>
    
    <body>
    <form name="testForm">
    <input type="text" name="what"><br>
    <input type="text" name="ever"><br>
    <input type="button" value="Create variable!" onClick="makeVar()"><br>
    <input type="button" value="Alert myVar" onClick="alert(myVar)">
    </form>
    </body>
    </html>
    Since you really didn't mention what you want the value of this variable to be, this example assumes that it is a concatenation of the two form fields.

    Note: In this particular example, the variable created can ALSO be accessed by it's acutal name, whatever, but as you state, this is a value you will not know.
    Last edited by beetle; Oct 15, 2002 at 07:11.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  11. #11
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beetle,

    "Yes, all your code works fine, my mistake."

    applied to your initial code as well, but that needed prior knowledge of the values of the variables.

    I've tried your new code, but I'm confused when you say that I can access the variable using whatever. How do I do this?

    Changing the line

    <input type="button" value="Alert myVar" onClick="alert(myVar)">

    to

    <input type="button" value="Alert myVar" onClick="alert(whatever)">

    doesn't seem to work. Am I missing something here? Many thanks for your effort so far.

  12. #12
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoop. Sorry...missing one line...
    Code:
    <html>
    <head>
    <title>The unknown variable</title>
    
    <script language="javascript">
    function makeVar() {
    	var s1 = document.forms['testForm'].elements[0].name;
    	var s2 = document.forms['testForm'].elements[1].name;
    	window[s1+s2] = document.forms['testForm'].elements[0].value + document.forms['testForm'].elements[1].value;
    	window.myVar = window[s1+s2];
    
    	}
    </script>
    </head>
    
    <body>
    <form name="testForm">
    <input type="text" name="what"><br>
    <input type="text" name="ever"><br>
    <input type="button" value="Create variable!" onClick="makeVar()"><br>
    <input type="button" value="Alert myVar by myVar" onClick="alert(myVar)"><br>
    <input type="button" value="Alert myVar by whatever" onClick="alert(whatever)">
    </form>
    </body>
    </html>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  13. #13
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you've got it there Beetle, many thanks ... hail to the king, baby ...


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
  •