SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member Mustals's Avatar
    Join Date
    Oct 2000
    Location
    Bolton
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I pass arrays to multiple functions(Javascript)

    I am trying to pass an (Global) array of variables to a number of functions in javascript but they are not being recognised.
    As follows:
    <Head>
    <Script>
    var nutherVariable ="Whatever"
    arrayset1 = newArray(n1)

    arrayset1[0] ="XXXXXX"
    arrayset1[2] ="XXXXXX"
    etc

    arrayset2 = newArray(n2)

    arrayset2[0] ="yyyyyyy"
    arrayset2[2] ="yyyyyyy"
    etc

    function doSomthing(arrayset1, arrayset2, nutherVariable){
    document.theForm.txtBox.value=arrayset1[n] + arrayset2[n];
    <!-- No output to form textfield unless array defined within function-->
    }
    </Script>
    </Head>
    <Body>
    Passes and recieves variables to/from function(s) via Form
    </Body

    I think what I am asking is what is the syntax to do this.
    Mustals

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2001
    Location
    Los Angeles
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to reference the element directly:
    array[0], array[1], etc...

    A global array is available to your function, and you don't need to do anything special to access it.
    bsacks
    http://www.middlepeak.com

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since I'm not exactly sure what you're after, I'll try to nail the three options that I can think of.
    When specifying variable names for arguments passed to a function, it is best to use unique names to keep things clear. Still, those variable names are local to the function and cannot be used elsewhere.
    Code:
    function doSomething(arg_1, arg_2, arg_3)   {
        document.the_form.txt_box.value = arg_1 + arg_2;
    }
    doSomething(my_array[0],my_other_array[0],"foo");
    The above passes one element each from the two arrays to be used locally by the function.

    If you want to pass the entire array to the function then you would do something like the following and then access the array elements by their indicies.
    Code:
    function doSomething(arry_1, arry_2)   {
        // two arrays are passed to the function
        // so to access any one element you must specify the index
        document.the_form.txt_box.value = arry_1[0] + arry_2[0];
    }
    doSomething(my_array, my_other_array);
    Passing variables and arrays to the function is generally a good idea because JavaScript makes a copy of the var or array so you do not risk accidental alteration of your global.
    Or you could do something as simple as this.
    Code:
    var my_array = new Array("foo", "bar");
    var my_other_array = new Array("cheese", "man");
    function doSomething()  {
         document.the_form.txt_box.value = my_array[0] + my_other_array[0];
    }
    <a href="javascript:doSomething();">Do Something</a>
    Your arrays are global and so do not need to be passed to the function. In this case when someone clicked the link, the text box would be updated with "foocheese." The first element from the first array + the first element from the second array.
    Hope this helps.
    Last edited by johnnydaboy; Jun 19, 2001 at 18:01.
    <<< johnny chidiac
    johnnydaboy@mac.com


Bookmarks

Posting Permissions

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