Reference A Variable Value With Another Variable
I want to store some number values as variables and then reference them by combining other variables together but I can't insert the values into inner html, only the variable names
Solution 1:
Store codes in object instead of separate variable. Then use bracket notation to construct object key from variables:
var codes = {
a1: 2.00,
a2: 4.00,
a3: 6.00
}
var input1 = $("#one").val(); // gives avar input2 = $("#two").val(); // gives 1, 2 or 3var code = codes[input1 + input2]; // combines input 1 and input 2 to give eg a2document.getElementById("output").innerHTML = code;
Solution 2:
You should use a object to store the values, the you can use Bracket notation to access property from the object obj
var obj = {
a1: 2.00,
a2: 4.00,
a3: 6.00
}
var input1 = 'a';
var input2 = '2';
var code = obj[input1 + input2]; //combines input 1 and input 2 to give eg a2console.log(code);
Post a Comment for "Reference A Variable Value With Another Variable"