javascript - Capturing variable names from a select -
in end have written code reads through file , outputs list of javascript arrays example, page see:
<script> var peanuts = ["1","s","g","3","n"]; var cashewnuts = ["d","a","f","d","n"]; var pecannuts = ["6","m","3","x","m"]; var brazilnuts = ["j","n","7","v","s"]; var goingnuts = ["a","e","7","m","y"]; </script>
i want use array based on value of somewhere else in page.
so example:
if($('select').val()===0){ alert(firstarray[1]); }
my issue variable names decided on contained in read file, can't know information. there way example
//collect value select , assign var var varn = $('select').val(); //then collect variable has variable name //equals value of 'varn'
i know seems horrendous unfortunately based on need do, need :(
yes. if example vars in global scope, can do
var val = window[varn][0];
peanuts:1
if
var nuts = { peanuts : ["1","s","g","3","n"], cashewnuts : ["d","a","f","d","n"], pecannuts : ["6","m","3","x","m"], brazilnuts : ["j","n","7","v","s"], goingnuts : ["a","e","7","m","y"] }
then can use
var val = nuts[varn][0];
Comments
Post a Comment