Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-02-2005, 10:10 PM   PM User | #1
doozer
New Coder

 
Join Date: Feb 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
doozer is an unknown quantity at this point
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.
doozer is offline   Reply With Quote
Old 03-02-2005, 11:01 PM   PM User | #2
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
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]
Antoniohawk is offline   Reply With Quote
Old 03-02-2005, 11:31 PM   PM User | #3
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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 :)
__________________
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.

Last edited by Mr J; 03-02-2005 at 11:50 PM..
Mr J is offline   Reply With Quote
Old 03-03-2005, 08:34 PM   PM User | #4
doozer
New Coder

 
Join Date: Feb 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
doozer is an unknown quantity at this point
Thumbs up Many thanks

Doh!

I knew it would be something simple.

It now works like a charm
doozer is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:11 AM.


Advertisement
Log in to turn off these ads.