The Little Guy Posted September 27, 2007 Share Posted September 27, 2007 OK... I am sending my function a word like: Art, Business, Games... etc. The problem is that when I print the word, to the screen it prints, but then when I try to do something with it like this: function buildSubCats(cat){ var lvl1Arts = Array(); lvl1Arts[0] = "Painting"; lvl1Arts[1] = "Digital Art"; lvl1Arts[2] = "Drawing"; lvl1Arts[3] = "Models"; lvl1Arts[4] = "Sewing"; lvl1Arts[5] = "Weaving"; lvl1Arts[6] = "Origami"; var len = eval("lvl1"+cat+".length"); for(var i=0;i<len;i++){ opt += '<option id="'+eval("lvl1"+cat[i])+'" value="'+eval("lvl1"+cat[i])+'">'+eval("lvl1"+cat[i])+'</option>'; } } If I were to send the word Art I would get an error saying: lvl1A is not defined First of all where does A come from? A should be Arts, not A Second it only does the first letter for some of the options, the ones with only one letter after lvl1, have sub arrays, otherwise it shows the full thing (lvl1Books). Why is it doing this? The page: http://secret.publicsize.com/advanced Quote Link to comment Share on other sites More sharing options...
emehrkay Posted September 27, 2007 Share Posted September 27, 2007 eval is evil. Are you using a multidimensional arrays? if so, use a hash/object instead of an array. then you wont need to use eval to get to the sub array let me see what i can do for you function buildCat(cat){ var lvl1 = { 'Arts': ['Painting', 'Drawing'] }; for(arr in lvl1[cat] ){ console.log(lvl1.Arts[arr]); } } console.log only works in ff if you have firebug installed. try alert() instead if you do not have that set up. but from my example you should be albe to build out your select box. notice how i use {} [] as shorthand for new Object or new Array - that is in the interest of saving time and processing, there is no need to call the object Quote Link to comment Share on other sites More sharing options...
nogray Posted September 28, 2007 Share Posted September 28, 2007 You forgot the new operator = new Array(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.