PDA

View Full Version : Assign an input value to become the name of the var



sniperman
01-14-2011, 10:17 AM
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.

Nile
01-14-2011, 12:46 PM
http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri
That should help you get starte

sniperman
01-14-2011, 02:16 PM
Thanks Nile.

I also found a similar tutorial here (http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript) and have since used it effectively.


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.

Nile
01-16-2011, 01:06 AM
No problem, I'm glad to help :D

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"