I have a select dropdown that on change I want it to run a function and pass along the select's value into that function. However, the function is expecting a variable holding an array, and what's being passed in is a string (the select's value).
You can see it working (or not working) in the code below. The selected value should represent its corresponding variable.
How can I accomplish this?
var str = ["Paul", "John", "Melissa", "Mike"];
var int = [8, 4, 55, 7];
var mix = ["Paul", 9, "John", 5, "Melissa", 67, "Mike", 2];
var main = document.querySelector('#main');
var sel = document.querySelector('select');
function testFunc(arr){
arr.forEach((i) => {
var p = document.createElement('p');
p.innerHTML = i;
main.appendChild(p);
});
}
testFunc(mix);
sel.addEventListener('change', testFunc(sel.value));
<select>
<option value="str">String</option>
<option value="int">Integer</option>
<option value="mix">Mixed</option>
</select>
<div id="main"></div>
EDIT:
I should note that the data arrays are coming in dynamically and I really would like to avoid manipulating them as to not cause additional code and confusion in future edits.