View Full Version : Selecting an array to use

03-02-2005, 10:10 PM

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. :)

03-02-2005, 11:01 PM
I'm not really a javascript guy, but I think that you want to look into square bracket notation.


Mr J
03-02-2005, 11:31 PM
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

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

document.getElementById("display"+i).innerHTML=window['my_array' + n][i]


// -->

<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>


Java is not javascript :)

03-03-2005, 08:34 PM

I knew it would be something simple.

It now works like a charm :)