Switch from text to variable name

-1

I have a textbox in which the user inserts a text and then I would like, by code, to create a variable with that text. How could I do it?

    
asked by Quarkbite 11.10.2017 в 11:39
source

2 answers

0

You have to use an array to do it:

$variable = [];

$input = $('.texto').text();

$variable[$input] = "negro";

console.log($variable[$input]);
console.log($variable["labrador"]);
console.log($variable.labrador); // que es la opcion que buscaba y que tambien se puede hacer con objetos.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textbox class="texto">labrador</textbox>
    
answered by 11.10.2017 / 11:42
source
0

Cheshire: indeed what I wanted was the first thing, to create a variable that was named just like what the user had inserted in the texbox.

I have solved it by creating an empty object and then adding a property with the name that was inserted in the texbox.

    
answered by 11.10.2017 в 12:17