Results 1 to 4 of 4

Thread: Assign an input value to become the name of the var

  1. #1
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Assign an input value to become the name of the var

    Hi all,

    I have a script that takes a user's input as a string. I would like to use that user's argument to become the name of a new variable.

    <script type="text/javascript">
    var input=prompt("enter any string value");
    var input.value; // initialize a new JavaScript variable using the user input as the referenced value
    </script>

    I know this script above doesn't work.
    Last edited by sniperman; 01-14-2011 at 12:07 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Jeremy | jfein.net

  3. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default

    Thanks Nile.

    I also found a similar tutorial here and have since used it effectively.

    Code:
    		x=0; // reset iteration	
    		var input=prompt("enter any string value");
    		var array = new Array("obj1","obj2","obj3");			  
    	
    		window[input] = new Array(); 
    			
    				for(i=array.length;i>0;i--)
    				  {
    					window[input][window[input].length]=
    array[x];
    					x++;
    				  }
    				  }
    It's always nice to learn something new knowing it's there to be discovered somewhere.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, I'm glad to help

    Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
    1. Go to your first post
    2. Edit your first post
    3. Click "Go Advanced"
    4. In the dropdown next to the title, select "RESOLVED"
    Jeremy | jfein.net

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
  •