Dynamic variable name in Javascript?

Solved
Tiret -  
 Danydan123 -
Hello

after going through the course sections without finding an answer, I turn to you.

Is it possible to give a dynamic name to a variable?

The JavaScript equivalent of:

 <?php $Nom_Var = Valeur; $Nom1 = 'Nom_Var'; echo $$Nom1; // Displays 'Valeur' ?> 


I'm waiting for your responses, please ask for clarification if I haven’t been clear enough!

12 answers

Tiret
 
I don't want to use PHP, I was just giving it as a comparison/example ;) But thanks for the reply.

Well, I managed to handle this while staying "javascript", here's my solution for those who might need it:

 function Test(Element) { // Let's assume Element contains 'Wood' var Price = 'Price_'+Element; // Price now contains 'Price_Wood' document.Form.Formula.eval(Price).value = 5000; // The value of the element Price_Wood in the Form is now set: it is now 5000. } 


Hoping this can help others!
38
jona303 Posted messages 390 Status Member 28
 
EDIT: nothing :)
0
Dronoïde
 
Indeed, the eval() function is made for that...
Thank you for this useful reminder, I had completely forgotten about that one ^^

On the other hand, I'm not at all convinced by the method using the window object mentioned below.
0