0

I want to be able to select from multiple sets of data using a select dropdown. All my code looks for one variable to pull the data from the selected array. I have defined my arrays and when I set my variable to use the data from an array it works throughout my code. When I try to change the array in the variable with a function instead of selecting the array the variable is set to the name of the array.

HTML

<select id="panelType" onchange="selectPanel(value)">
    <option value="S5">S5</option>
    <option value="X6">X6</option>
    <option value="F4">F4</option>
</select>

Javascript

var S5 = [.5, .5, 96, 96, 16.3, 1.05, 0, .5, 3, 0, 0];
var X6 = [.5, .5, 72, 72, 21.2, 1.05, 1, 4, 40, 1, 18.1];
var F4 = [.5, 1, 104, 208, 28.4, 2.05, 1, 2, 17.6, 1, 9];

var pData = X6;
alert(pData[3]);

function selectPanel(pType) 
{
    pData = pType;
    alert(pData[2]);
}

The first alert returns 208, but the alert in the function returns undefined. If I were to change it to alert(pData); it would return S5 if I selected the first option.

  • `pData = window[pType];` – 4castle Nov 19 '16 at 22:24
  • I don't think that does what you think it does. You will assign the _string_ `"F4"` (or whichever) as `pData` not the _array_ referenced by the variable `F4`. Doing `[2]` does not throw an error by accident, since it can also be used to get a character from a string and the strings do not have a third character (index 2), so you get `undefined`. – VLAZ Nov 19 '16 at 22:25
  • @4castle that solved it, thank you! – NoodleNick Nov 19 '16 at 22:27

0 Answers0