...

View Full Version : Problem passing an array variable to a function



cenze
10-26-2006, 06:58 PM
Hi Everyone!

Here is a little script I'm working on:


function draw_list(array)
{
var output = "<ul id=\"nav\"><li>Select a lab:<ul>";
for (i=0; i < array.length; i++)
{
output += "<li><a href=\"#\" onclick=\"change_lab(" +
array[i][1] + ")\">" + array[i][0] + "</a></li>";
}
ouput += "</ul></li></ul>";
document.getElementById('dd_list').innerHTML = output;
}
Here is the array with list text and array names:

var period3 =
[
['Bone Joint and Skin Lab 01', lab01],
['Bone Joint and Skin Lab 02', lab02],
['Bone Joint and Skin Lab 03', lab03]
]
I am basically trying to create a dropdown list from array values.
The change_lab() function receives an array, which is 'contained' in array[i][1]. However, change_labs doesn't seem to be getting the array unless I explicitly plug it in like:

output += "<li><a href=\"#\" onclick=\"change_lab(lab02)\">" + period[i][0] + "</a></li>";
Can anyone tell me why, exactly, that this is the case?
I have a lot to learn about js mechanics...
Thanks very much!

Arty Effem
10-27-2006, 12:12 AM
Hi Everyone!

Here is a little script I'm working on:


function draw_list(array)
{
var output = "<ul id=\"nav\"><li>Select a lab:<ul>";
for (i=0; i < array.length; i++)
{
output += "<li><a href=\"#\" onclick=\"change_lab(" +
array[i][1] + ")\">" + array[i][0] + "</a></li>";
}
ouput += "</ul></li></ul>";
document.getElementById('dd_list').innerHTML = output;
}
Here is the array with list text and array names:

var period3 =
[
['Bone Joint and Skin Lab 01', lab01],
['Bone Joint and Skin Lab 02', lab02],
['Bone Joint and Skin Lab 03', lab03]
]
I am basically trying to create a dropdown list from array values.
The change_lab() function receives an array, which is 'contained' in array[i][1]. However, change_labs doesn't seem to be getting the array unless I explicitly plug it in like:

output += "<li><a href=\"#\" onclick=\"change_lab(lab02)\">" + period[i][0] + "</a></li>";
Can anyone tell me why, exactly, that this is the case?
I have a lot to learn about js mechanics...
Thanks very much!Try it like this:
output += "<li><a href=\"#\" onclick=\"change_lab(array[" + i + "][1])\">" + array[i][0] + "</a></li>";

Beagle
10-27-2006, 03:25 PM
the reason is because array[i][1] is not a string value, but a variable. I don't know what it contains, but it's not a string. What you need to do is change your array to the following:



var period3 =
[
['Bone Joint and Skin Lab 01', 'lab01'],
['Bone Joint and Skin Lab 02', 'lab02'],
['Bone Joint and Skin Lab 03', 'lab03']
]

cenze
11-01-2006, 09:29 PM
Thanks Beagle, that was the problem :thumbsup:

Thanks also to Arty for the reply :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum