The method eval();
is an option.
Perhaps the only answer here that will work for the non-global variables. Although eval();
should not be used in this case as there are better methods.
Speaking of better methods you can use the object window
has a reference to all global variables and global functions you are using.
But if you don’t want to use one method eval();
or object window
. you can try something like this:
<script language="javascript">
<!--
var obj = {};
obj['teste'] = 'Não há mais necessidade de armazenar em um objeto global.\nO segredo fica por conta do acesso a propriedades via string.';
alert(obj['teste']);
//-->
</script>
In Javascript these two are equivalent:
obj.teste = 'valorString'
obj['teste'] = 'valorString'
To illustrate a little better the operation of some of these methods/objects, created examples that use some examples to demonstrate the use of dynamic variables
Example 1
<script language="javascript">
<!--
var nome;
nome = window.prompt("Digite o nome:");
var x = nome;
var valor = 'x';
// resultado
alert(eval(valor));
//-->
</script>
Example 2
<script language="javascript">
<!--
var nome;
nome = window.prompt("Digite o nome");
var x = 'valor';
str = x+' = '+'nome';
eval(str);
// resultado
alert(valor);
//-->
</script>
If you need to create dynamic variables in Javascript, and you don’t know how many variables you’ll need. Use the syntax for
, make a loop with the method eval();
. The method eval();
receives as parameter a String which will be executed as if it were a command. See:
Example 1
<script language="javascript">
<!--
var nome;
nome = window.prompt("Digite o nome:");
for(var i = 1; i <= 3; i++)
{
window['v' +i] = "var " + i;
}
// recuperando as variaveis
alert(v1); // variavel 1
alert(v2); // variavel 2
alert(v3); // variavel 3
//-->
</script>
Example 2
<script language="javascript">
<!--
//criando variável dinâmica
var valor;
valorTeste = window.prompt("Digite o nome:");
for(var i = 0; i < 3; i++) {
eval("nome" +i+ " = '"+valor+"';");
//recuperando os valores dessas variáveis
alert(window["nome" +i]);
}
//-->
</script>
Example 3
Now, let’s create dynamic variables using the object window
javascript:
<script language="javascript">
<!--
//criando algumas variáveis dinamicamente
for (var i = 0; i < 5; i++)
{
window["nome" + i] = i + 10;
}
//recuperando os valores dessas variáveis
for (var i = 0; i < 5; i++)
{
alert(window["nome" + i]);
}
</script>
Example 4
<script language="javascript">
<!--
//criando variável dinâmica
var nome;
nome = window.prompt("Digite o nome:");
for(var i = 0; i < 3; i++) {
alert(eval+"(var "+nome+i+" = '"+i+"';)");
}
//-->
</script>
These examples are very simple, although impractical.
Hello Diego. The question is unclear. You have one
<input type="text">
correct? and then you want the value inserted there to go to a variable right? you can explain better what happens next?– Sergio
Diego, jump in here: http://chat.stackexchange.com/rooms/25038/javascript
– Sergio
I added an answer below, to see if it helps. If you have problems accessing chat because your browser downloads Chrome or Firefox and tests again.
– Sergio
Are you sure you need to create variables with the dynamic name like this and it would not be enough to use fields from a global table, like
var minhatabela = {}; minhatabela[nomedocampo] = valordocaompo
? If you quit overwriting global variables you always have a chance to spoil the value of some existing variable.– hugomg