Click to See Complete Forum and Search --> : Logic problem (I think)


Ben Franklin
09-04-2003, 11:57 AM
OK, this may sound a little convoluted; so I hope you all can stil understand my question.

I'm trying to construct a customizable drop-down menu. I have several global variables defined at the top of the script:

var strMenus = "Menu One=#,Menu Two=#,Menu Three=#,Menu Four=#,Menu Five=#,Menu Six=#"

var menu1= "Menu Item One=#,Menu Item Two=#,Menu Item Three=#,Menu Item Four=#,Menu Item Five=#,Menu Item Six=#"
var menu2= "Menu Item One=#,Menu Item Two=#,Menu Item Three=#,Menu Item Four=#,Menu Item Five=#,Menu Item Six=#"

etc ...


I can split the first line into an array.(As there is only 1 menu header ever and the variable name is known - 'strMenus') Then I'm using the array data to contstruct the menu header. No problem.

But when I get down into the for loop that is constructing the menu and subsequent drop-down DIVs, I am dealing with an unknown number of potential drop-downs. I can get it -- as the length of the strMenus array, but how can I 'dynamically' refer to a global variable? (or local)

I can pass the number of the menu, and of course they're all prefixed with 'menu' -- but I do not understand how I can refer to the variable 'menu1' and then 'menu2' then 'menu3' - etc.

Does this question make sense? Any help would be greatly appreciated.

Ben Franklin

karayan
09-04-2003, 12:49 PM
I'm not sure I fully understand your problem. Are you saying that you need to work with objects whose name may change with the program logic? For example, working with lengths of arrays whose name depends on the logic? If yes, the eval() command is invaluable for this. Form the Javascript command you want as a string, using program logic, and then eval() the string.

Ben Franklin
09-04-2003, 01:36 PM
George,

Thanks for your response. To tell the truth I'm not sure what I mean, but I'll try to elaborate.

In simplest terms:
I need to perform a split on a predefined variable inside of a for loop. These variables are defined at the top of the page. (to allow other users to customize) the format of these variable names is always the same.(menu1, menu2, menu3, etc)

The problem is incrementally referring to these menus as the loop progresses.

tmpArray = menu(i).split(",")

Obviously this line doesn't work but I hope it illustrates my problem.

I've attached the code as a text file -- there's not much there yet, so looking at it may be easier to figure out then describing it.

Thanks again!

Kevin

Ben Franklin
09-04-2003, 02:58 PM
I've got it!

It seems I was trying to do in one step that which logically takes two steps.

tmpVarIdent = eval("menu" + (i + 1))
tmpArray = tmpVarIdent.split(",")

Thanks for the help!

karayan
09-04-2003, 10:01 PM
Yes, that's what I had in mind when I recommended the eval() function. Whoever first thought of this function was a genius!:cool:

karayan
09-04-2003, 10:11 PM
Yes, that's what I had in mind when I recomended the eval() function. Whoever first thought of this function was a genius!:cool: