0

If you're tempted to say this has been answered by the SO answer: 5117127 please don't - it doesn't help me out. That answer says I can use the window object to access it: window['varToReturn'] - this is not working.

A basic form:

<form id="formID">
     <input type="number" id="inputID"/>
</form>

I have basic variables with values:

var varName1 = 1 ;
var varName2 = 2 ;

Then am using a function to get the appropriate variable name to return:

$("#formID").submit(function(e){
            e.preventDefault();
            var inputValue= $("#inputID").val();
            
            var varToReturn = 'varName' + inputValue;

            console.log(varToReturn);
    });

It's returning 'varToReturn1' - not the variable value (which is just '1')...am sure it's probably a simple thing, but help would be appreciated.

timpng1
  • 148
  • 1
  • 8
  • 2
    What you need is **one** variable, whose value is an **array**. Then all you need to do is index into the array with the input value. – Pointy May 07 '21 at 17:56
  • I've tried that - but when I index into it it's returning the index of the array_name and not the value. Similar problem. – timpng1 May 07 '21 at 17:59
  • Why do you have varName1 and varName2 declared if you're not going to be using them? – Chris May 07 '21 at 18:04
  • I'm pulling the vars from a CSV file via PHP and tried an array - but had a similar problem with the array_name indexing the name itself and not the value - someone closed this saying it's been answered but the answer is not the answer...would like some help still. – timpng1 May 07 '21 at 18:11

0 Answers0