Forum Moderators: open

Message Too Old, No Replies

JS Array error

         

ahmedtheking

11:41 am on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the code:

// languages
var lang1 = 'English'
var lang2 = 'Irish'

// arrays for each menus
var themenus = new Array()
themenus[0] = 'tutorial_lang_new'
themenus[1] = 'tutorial_lang_1'
themenus[2] = 'tutorial_lang_2'
themenus[3] = 'tutorial_lang_3'
themenus[4] = 'tutorial_lang_4'

// contents for menu in foreach
for (x=0; x < 5; x++) {
var themenus[x] = new Array()
themenus[x][0] = '<a href="window.php?get='+themenus[x]+'&amp;lang='+lang1+'">'+lang1+'</a>'
themenus[x][1] = '<a href="window.php?get='+themenus[x]+'&amp;lang='+lang2+'">'+lang2+'</a>'
}

Here's what I'm trying to do:

I'm trying to get JS to declare each part of 'themenus' array as a var (like a foreach in PHP), but I know I've done it wrong, can anyone help me?

ahmedtheking

12:16 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's ok, sorted it!

texmex

12:48 am on Jun 22, 2006 (gmt 0)

10+ Year Member



Just one little tip. To allow you to add to that array, without having to re-write your code every time you could change your loop to read:

for (x=0; x < themenus.length ; x++)

Also, to save you having to keep count of how many elements there are, you could define the array as:


var themenus = new Array(
'tutorial_lang_new',
'tutorial_lang_1',
'tutorial_lang_2',
'tutorial_lang_3',
'tutorial_lang_4'
);