I have a couple JavaScript arrays:
var someMotionToys = ["cars", "bikes", "surfboards", "skis"];
var immobileToys = ["xbox", "guitar", "paintbrush", "laptop", "crossword puzzles"];
var rareToys = ["ironmaiden", "pogostick", "lawndarts"];
I have an HTML 'select' tag as follows:
<select id="toys" onchange="handleToysChoice(this)">
<option value="someMotionToys">toys in motion</option>
<option value="immobileToys">toys for inside</option>
<option value="rareToys">antique toys</option>
</select>
I handle the onchange JavaScript function here:
function handleToysChoice(theToysSelector)
{
var theToySelection = theToysSelector.value;
// AT THIS POINT -- assume the user selected 'toys in motion'
// so 'theToySelection' here is "someMotionToys" -- that is *also*
// the name of my array -- but the following tells me it's not
// referencing the array:
alert("The number of elements in the array " + theToySelection
+ " is: " + theToySelection.length);
}
The 'length' value I get is the length of the word 'someMotionToys' which, despite being the name of my array, is also the name of the 'value' from the html 'select' box. In other words, the 'length' value I see is 14 (the number of letters in 'someMotionToys') but I need the 'length' to be 4, the number of elements in my 'someMotionToys' array. I need this 'value' from the HTML select box to be 'connected' to my array. I want to store my array names in select boxes, in their 'value' fields, then easily extract the 'value' in the onchange handler and quickly reference the right array.
How do I 'coerce' the text string I get from a value out of an HTML 'select' so that it is mapped to my array name?
I need to store array names in several 'selects' on my web page -- my plan was to store the array names in the 'value' of each option in the 'select' boxes.
How can I make this work? I have no plans to use jQuery by the way.
var theToySelection = eval(theToySelector.value), I think this will return your array