Dynamic variable name in Javascript?

Solved
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

  1. 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
    1. EDIT: nothing :)
      0
    2. 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
    3. Hello,

      and thank you for the help. It's clear and very useful.
      0
    4. Instead of:
      document.Formulaire.eval(Prix).value = 5000;
      we can use:
      document.Formulaire[Prix].value = 5000;
      because sometimes eval does not work.
      0
    5. well it's too late now but I would have rather done a document.createElement
      0