Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem passing an array variable to a function

    Hi Everyone!

    Here is a little script I'm working on:

    Code:
    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:
    Code:
    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:
    Code:
    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!
    Last edited by cenze; 10-26-2006 at 07:06 PM. Reason: title edit

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by cenze View Post
    Hi Everyone!

    Here is a little script I'm working on:

    Code:
    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:
    Code:
    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:
    Code:
    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:
    Code:
    output += "<li><a href=\"#\" onclick=\"change_lab(array[" + i + "][1])\">" + array[i][0] + "</a></li>";

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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:

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

  • #4
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Beagle, that was the problem

    Thanks also to Arty for the reply


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •