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 Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Selecting an array to use

    Hi

    I have declared a series of arrays each containing 18 elements.

    I want to write some java that will allow me to write the array elements into a html table for displaying on screen.

    I have named the arrays srch1, srch2, srch3 etc.

    I want to pass a number into the function that displays the elements and say use array srch? where ? is the number I've passed in.

    I am not an experienced programmer and I thought I could just do something like

    function FillTable (n) {
    target = 'srch'+n;
    document.all(NameCell).innerHTML = target[0];
    document.all(NameCell).innerHTML = target[1];

    but I assume the code looks for an array called target and gives me undefined for all the values.

    I know the answer to this is probably obvious but please be gentle.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I'm not really a javascript guy, but I think that you want to look into square bracket notation.

    [http://www.litotes.demon.co.uk/js_info/sq_brackets.html]

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Please try the following example.

    The first loop in the script is soley for clearing the divs for this example, the important bit is in bold



    <script language="javascript">
    <!--
    my_array0=new Array("a")
    my_array1=new Array("Hello","World")
    my_array2=new Array("1","2","3")
    my_array3=new Array("Once","upon","a","time")
    my_array4=new Array("Jan","Feb","March","April","May")

    function show_me(n){

    for(i=0;i<5;i++){ // clear divs
    document.getElementById("display"+i).innerHTML=""
    }

    array_length=window['my_array' + n].length

    for(i=0;i<array_length;i++){
    document.getElementById("display"+i).innerHTML=window['my_array' + n][i]
    }

    }

    // -->
    </script>


    <a href="#null" onclick="show_me(0)">Show me 1</a><BR>
    <a href="#null" onclick="show_me(1)">Show me 2</a><BR>
    <a href="#null" onclick="show_me(2)">Show me 3</a><BR>
    <a href="#null" onclick="show_me(3)">Show me 4</a><BR>
    <a href="#null" onclick="show_me(4)">Show me 5</a><BR>

    <div id="display0"></div>
    <div id="display1"></div>
    <div id="display2"></div>
    <div id="display3"></div>
    <div id="display4"></div>


    PS

    Java is not javascript :)
    Last edited by Mr J; 03-02-2005 at 11:50 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #4
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Many thanks

    Doh!

    I knew it would be something simple.

    It now works like a charm


  •  

    Posting Permissions

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