...

View Full Version : Selecting an array to use



doozer
03-02-2005, 11:10 PM
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. :)

Antoniohawk
03-03-2005, 12:01 AM
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]

Mr J
03-03-2005, 12:31 AM
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 :)

doozer
03-03-2005, 09:34 PM
Doh!

I knew it would be something simple.

It now works like a charm :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum