473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Addressing an Array from Function Arguments?

Hi,

I desperately need some help with what I assume to be a minor problem
that I just can't solve.

I'm writing a page where on mouseover a link, an image needs to be
displayed and some text, which is defined inside an array, sould
appear inside a table cell (whose id is "tdDesc").

That's basically what's in the HTML-file:

<body onload="resetDesc('WS_Standard','3');">
<a href="#" onmouseover="showDesc('WS_Standard','1');"
onmouseover="showDesc('WS_Standard','1');">
Link no. 1</a>

Here's what's in the js-File.

01 function showDesc(typeDesc,noDesc) {
02 var imgDesc = ('img' + typeDesc + '_' + noDesc);
03 document.getElementById('tdDesc').innerHTML =
WS_Standard[noDesc];
04 document.getElementById(imgDesc).style.display = "";
05 }
06 function hideDesc(typeDesc,noDesc) {
07 var imgDesc = ('img' + typeDesc + '_' + noDesc);
08 document.getElementById('tdDesc').innerHTML = WS_Standard[0];
09 document.getElementById(imgDesc).style.display = "none";
10 }
11 function resetDesc(typeDesc,totalDesc) {
12 for (var i = 1; i <= totalDesc; i++) {
13 var imgDesc = ('img' + typeDesc + '_' + i);
14 document.getElementById(imgDesc).style.display = "none";
15 }
16 document.getElementById('tdDesc').innerHTML = WS_Standard[0];
17 }
18 var WS_Standard = new Array();
19 WS_Standard[0] = "Standard text when no mouse over";
20 WS_Standard[1] = "Sample Text 1";
21 WS_Standard[2] = "Sample Text 2";
22 WS_Standard[3] = "Sample Text 3";

My problem appears when it comes to adressing the text that is in the
array, lines 03, 08, 16. I want to use my "typeDesc"-argument to
adress the array, but whatever I try, it's either an error or I get a
string "WS_Standard" or "WS_Standard[1]" or something, but never the
text in the array WS_Standard[1].

What am I doing wrong?

Any help is highly appreciated!

Martin
Jul 23 '05 #1
13 1556
Martin Altschwager <co***@altschwager.de> wrote in message
news:bb**************************@posting.google.c om...
onmouseover="showDesc('WS_Standard','1');" 01 function showDesc(typeDesc,noDesc) {
03 document.getElementById('tdDesc').innerHTML = WS_Standard[noDesc];


You're subscripting the array with the string: '1';

--
S.C.

Jul 23 '05 #2
In article <42**********@mk-nntp-2.news.uk.tiscali.com>,
"Stephen Chalmers" <ig******@lycos.co.uk> wrote:
Martin Altschwager <co***@altschwager.de> wrote in message
news:bb**************************@posting.google.c om...
onmouseover="showDesc('WS_Standard','1');"

01 function showDesc(typeDesc,noDesc)

{
03 document.getElementById('tdDesc').innerHTML = WS_Standard[noDesc];


You're subscripting the array with the string: '1';


While it isn't standard, that should still work since numeric indexes
are converted a string value before the actual lookup.

Robert
Jul 23 '05 #3
In article <bb**************************@posting.google.com >,
co***@altschwager.de (Martin Altschwager) wrote:
Any help is highly appreciated!


I do not believe that I found any problems with the javascript functions
you showed. I filled in the missing html and changed one event handler.

Here is my revised version of the file.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Saving stats</title>
<base href="http://spaceplace.jpl.nasa.gov/en/_images/">
<script type="text/javascript">
/*
My problem appears when it comes to adressing the text that is in the
array, lines 03, 08, 16. I want to use my "typeDesc"-argument to
adress the array, but whatever I try, it's either an error or I get a
string "WS_Standard" or "WS_Standard[1]" or something, but never the
text in the array WS_Standard[1].

changes
call onmouseout
call hideDesc
you do not show the text to change nor the images. Could have the
wrong values in the id attributes
this is a bad variable name tdDesc 'cause it is hard to distingish
the d from the D.

*/
var WS_Standard = new Array();
WS_Standard[0] = "Standard text when no mouse over";
WS_Standard[1] = "Sample Text 1";
WS_Standard[2] = "Sample Text 2";
WS_Standard[3] = "Sample Text 3";
function showDesc(typeDesc,noDesc) {
var imgDesc = ('img' + typeDesc + '_' + noDesc);
document.getElementById('tableDescription').innerH TML =
WS_Standard[noDesc];
document.getElementById(imgDesc).style.display = "";
}
function hideDesc(typeDesc,noDesc) {
var imgDesc = ('img' + typeDesc + '_' + noDesc);
document.getElementById('tableDescription').innerH TML =
WS_Standard[0];
document.getElementById(imgDesc).style.display = "none";
}

function resetDesc(typeDesc,totalDesc) {
for (var i = 1; i < totalDesc; i++) {
var imgDesc = ('img' + typeDesc + '_' + i);
document.getElementById(imgDesc).style.display = "none";
}
document.getElementById('tableDescription').innerH TML =
WS_Standard[0];
}
</script>
</head>
<body onload="resetDesc('WS_Standard',WS_Standard.length );">
<a href="#" onmouseover="showDesc('WS_Standard',1);"
onmouseout="hideDesc('WS_Standard',1);">
Link no. 1</a>

<p>
<img
id="imgWS_Standard_1"
src="common/nasa_header/logo_nasa.gif">
<br>
<img
id="imgWS_Standard_2"
src="common/nasa_header/logo_nasa.gif">
<br>
<img
id="imgWS_Standard_3"
src="common/nasa_header/logo_nasa.gif">
</p>
<p>
<span id="tableDescription" class="hidden">Message to display</span>
</p>
</body>
</html>
Jul 23 '05 #4
"Stephen Chalmers" <ig******@lycos.co.uk> wrote in message news:<42**********@mk-nntp-2.news.uk.tiscali.com>...
Martin Altschwager <co***@altschwager.de> wrote in message
news:bb**************************@posting.google.c om...
onmouseover="showDesc('WS_Standard','1');"

01 function showDesc(typeDesc,noDesc)

{
03 document.getElementById('tdDesc').innerHTML = WS_Standard[noDesc];


You're subscripting the array with the string: '1';


Hi Stephen,

Thank you for answering, but I'm sorry, I don't get it. The way the
function is shown above, it does work. But what needs to be changed in
my function so that it returns the text in the array that I specify in
the argument? Something like

01 function showDesc(typeDesc,noDesc) {
03 document.getElementById('tdDesc').innerHTML = typeDesc[noDesc];

These two lines obviously don't work. What's wrong?

Thank you already for further help!

Martin
Jul 23 '05 #5
Hi Robert,

thank you for your reply. As a matter of fact, the .js-file I posted is
indeed working. My problem lies in lines 3, 8, and 16. Let me
demonstrate with line 3:

document.getElementById('tdDesc').innerHTML=WS_Sta ndard[noDesc];

I'm explicitly pointing to the Array WS_Standard, and that works
perfectly.

However, I want to use the function later again with another Array, for
instance WS_Special, so the "WS_Standard" in my line needs to be
replaced with the name that I put in the arguments of the calling
function, showDetail('WS_Special','1').

(Otherwise I would have to duplicate the stupid function as many times
as I have Arrays)

I've tried different things such as

document.getElementById('tdDesc').innerHTML=typeDe sc[noDesc];
document.getElementById('tdDesc').innerHTML=showDe sc.argument[0][noDesc]
;

and so on, but it either resulted in an error, or I got just portions of
the name itself as in

showDetail('WS_Standard','0') returned 'W'
showDetail('WS_Standard','1') returned 'S'
showDetail('WS_Standard','2') returned '_'

and so on...

Plain and simple: When the function called goes
showDetail('WS_Special','1');

what do I have to write here
document.getElementById('tdDesc').innerHTML=???[noDesc];

so that the function fills my tdDesc-Tablecell with the value of the
Array "WS_Special[1]"?

You help is highly appreciated!

Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #6
Hi Robert,

I just reread your revised code sample. As a matter of fact, when I use
this, there's another error coming up:

You're calling onload "resetDesc('WS_Standard',WS_Standard.length')"

WS_Standard.length returns 10, because "WS_Standard" consists of 11
characters. Here too, the string itself is addressed and not the Array
with that name. Why's that? Seems similiar to my particular problem...

Regards,

Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #7
Martin Altschwager wrote:
Hi,

I desperately need some help with what I assume to be a minor problem
that I just can't solve.

I'm writing a page where on mouseover a link, an image needs to be
displayed and some text, which is defined inside an array, sould
appear inside a table cell (whose id is "tdDesc").

That's basically what's in the HTML-file:

<body onload="resetDesc('WS_Standard','3');">
<a href="#" onmouseover="showDesc('WS_Standard','1');"
onmouseover="showDesc('WS_Standard','1');">
Link no. 1</a>
Since you creating your messages as a global array, why not pass
them directly?

<a href="#" onmouseover="showDesc(WS_Standard[1]);"
onmouseout="showDesc(WS_Standard[0]);">
[...] 18 var WS_Standard = new Array();
19 WS_Standard[0] = "Standard text when no mouse over";
20 WS_Standard[1] = "Sample Text 1";
21 WS_Standard[2] = "Sample Text 2";
22 WS_Standard[3] = "Sample Text 3";

This is simpler (to me) when written as:

var WS_Standard = [
"Standard text when no mouse over",
"Sample Text 1",
"Sample Text 2",
"Sample Text 3"
];

Both create exactly the same array.
--
Rob
Jul 23 '05 #8
RobG wrote:
That's basically what's in the HTML-file:

<body onload="resetDesc('WS_Standard','3');"
<a href="#"
onmouseover="showDesc('WS_Standard','1');"
onmouseover="showDesc('WS_Standard','1');"
Link no. 1</a>
Since you creating your messages as a global array, why
not pass them directly?

<a href="#" onmouseover="showDesc(WS_Standard[1]);"
onmouseout="showDesc(WS_Standard[0]);">


Thanks for answering. It's a good idea, however, that same function must
show or hide the corresponding image, too.

My image are called "imgWS_Standard_1" and so on (compare lines 2 and 4,
7 and 9, 13 and 14). I feel there must be an easier way than using the
argument "WS_Standard[1]" and then splitting it up again to make it
"imgWS_Standard"...

Any idea?
This is simpler (to me) when written as:

var WS_Standard = [
"Standard text when no mouse over",
"Sample Text 1",
"Sample Text 2",
"Sample Text 3"
];

Both create exactly the same array.


Maybe, but I like the numbers nearby so that I know instantly what text
corresponds to what number (think 20 entries). I believe it makes no
difference here...

Thanks!

Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #9
Martin wrote:
RobG wrote: [...]
Since you creating your messages as a global array, why
not pass them directly?

<a href="#" onmouseover="showDesc(WS_Standard[1]);"
onmouseout="showDesc(WS_Standard[0]);">

Thanks for answering. It's a good idea, however, that same function must
show or hide the corresponding image, too.


OK, you can still pass more parameters:

<a href="#" onmouseover="showDesc(WS_Standard[1],'1');"
onmouseout="showDesc(WS_Standard[0],'2');">

WS_Standard[1] will be evaluated so that your function
showDesc() gets the string (or whatever is in WS_Standard).

My image are called "imgWS_Standard_1" and so on (compare lines 2 and 4,
7 and 9, 13 and 14). I feel there must be an easier way than using the
argument "WS_Standard[1]" and then splitting it up again to make it
"imgWS_Standard"...

Any idea?

This is simpler (to me) when written as:

var WS_Standard = [
"Standard text when no mouse over",
"Sample Text 1",
"Sample Text 2",
"Sample Text 3"
];

Both create exactly the same array.

Maybe, but I like the numbers nearby so that I know instantly what text
corresponds to what number (think 20 entries). I believe it makes no
difference here...


Fine, just making a suggestion.

Here's another. You can create an object that contains your
arrays, then you can address them using strings passed to your
function (I think this fits better with what you are trying to
do). They are often incorrectly called "hashtables" or
"2D arrays":
<html><head>
<title>2d arrays</title>
<script type="text/javascript">

var WS_Standard = ["Standard text when no mouse over",
"Standard Text 1","Standard Text 2","Standard Text 3"];
var WS_Super = ["Super text when no mouse over",
"Super Text 1","Super Text 2","Super Text 3"];

var msgObj = {'WS_Standard': WS_Standard,'WS_Super':WS_Super};
function zap(aName,aIndex) {
return(msgObj[aName][aIndex]);
}

</script>
</head>
<body>
<input type="button" value="std 0" onclick="
alert(zap('WS_Standard','0'));
">
<input type="button" value="std 1" onclick="
alert(zap('WS_Standard','1'));
">
<input type="button" value="std 2" onclick="
alert(zap('WS_Standard','2'));
">
<input type="button" value="std 3 onclick="
alert(zap('WS_Standard','3'));
">
<br>
<input type="button" value="super 0" onclick="
alert(zap('WS_Super','0'));
">
<input type="button" value="super 1" onclick="
alert(zap('WS_Super','1'));
">
<input type="button" value="super 2" onclick="
alert(zap('WS_Super','2'));
">
<input type="button" value="super 3" onclick="
alert(zap('WS_Super','3'));
">
</body></html>
The above is just play code to demonstrate a "2d" array.

--
Rob
Jul 23 '05 #10
Martin Altschwager <co***@altschwager.de> wrote in message >
Hi Stephen,

01 function showDesc(typeDesc,noDesc) {
03 document.getElementById('tdDesc').innerHTML = typeDesc[noDesc];

These two lines obviously don't work. What's wrong?

Thank you already for further help!

Martin


I wish you had shown that code initially. In that case the problem is still
in the way you are passing the relevant parameter, assuming this function
call is unchanged:

<a href="#" onmouseover="showDesc('WS_Standard','1');"

which means that you end up trying to subscript the string typeDesc.

--
S.C.

http://makeashorterlink.com/?H3E82245A

Jul 23 '05 #11
Hi Rob,

you're a genius and you were right. It was exactly the
what-should-not-be-called-2D-arrays solution that I needed. (I had heard
of this, but never learnt how to do it.) I had it implemented within a
minute and it works perfectly.

Thank you very much!

Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #12
"Stephen Chalmers" <ig******@lycos.co.uk> wrote in message news:<42**********@mk-nntp-2.news.uk.tiscali.com>...
Martin Altschwager <co***@altschwager.de> wrote in message
news:bb**************************@posting.google.c om...
onmouseover="showDesc('WS_Standard','1');"

01 function showDesc(typeDesc,noDesc)

{
03 document.getElementById('tdDesc').innerHTML = WS_Standard[noDesc];


You're subscripting the array with the string: '1';


My problem's been solved. I'm using 2D-Arrays now.

Thanks!

Martin
Jul 23 '05 #13
In article <42**********@127.0.0.1>, Martin <no*****@for.me> wrote:

I'm explicitly pointing to the Array WS_Standard, and that works
perfectly.

However, I want to use the function later again with another Array, for
instance WS_Special, so the "WS_Standard" in my line needs to be
replaced with the name that I put in the arguments of the calling
function, showDetail('WS_Special','1').

(Otherwise I would have to duplicate the stupid function as many times
as I have Arrays)


There are two things you are working with here. One is the array
WS_Standard and the other is the string of characters used in the id
attribute of the img tag. You have given them the similar names which
is confusing.

Here is a way of accessing multiple lists and images and text.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Saving stats</title>
<base href="http://spaceplace.jpl.nasa.gov/en/">
<script type="text/javascript">

/* I am create an object grouptArrays which contains the indexes
WS_Standard and WS_Special. Each index points to an arrary
with the desired text strings. I put in comments to note
the index in the array.

You can use the bracket notaion [ ] to access objects.

Trying to initialize lots of data via assignment statements
will lead to errors when you copy data to save typing and
when you change entries. */

/* I find your variable names confusing. noDesc could be no description
instead of number description. Arrarys take indexes so using index
over
number has more meaning.
*/

var groupArray = {'WS_Standard': [
/* 0 */ "Standard text when no mouse over",
/* 1 */ "Sample Text 1",
/* 2 */ "Sample Text 2",
/* 3 */ "Sample Text 3"
],
'WS_Special': [
/* 0 */ "special text when no mouse over",
/* 1 */ "special Text 1",
/* 2 */ "special Text 2",
/* 3 */ "special Text 3"
]
}
function showDesc(description,theIndex) {
var imgDesc = ('img' + description + '_' + theIndex);
document.getElementById('td_'+description).innerHT ML =
groupArray[description][theIndex];
document.getElementById(imgDesc).style.display = "";
}
function hideDesc(description,theIndex) {
var imgDesc = ('img' + description + '_' + theIndex);
document.getElementById('td_'+description).innerHT ML =
groupArray[description][0];
document.getElementById(imgDesc).style.display = "none";
}

function resetDesc(description,totalDesc) {
for (var i = 1; i < totalDesc; i++) {
var imgDesc = ('img' + description + '_' + i);
document.getElementById(imgDesc).style.display = "none";
}
document.getElementById('td_'+description).innerHT ML =
groupArray[description][0];
}
</script>
</head>
<body onload="resetDesc('WS_Standard',groupArray['WS_Standard'].length);
resetDesc('WS_Special',groupArray['WS_Special'].length);">

<a href="#" onmouseover="showDesc('WS_Standard',1);"
onmouseout="hideDesc('WS_Standard',1);">
Link number 1</a>

<p>
<img
id="imgWS_Standard_1"
src="_images/common/nasa_header/logo_nasa.gif">
<br>
<img
id="imgWS_Standard_2"
src="_images/common/nasa_header/logo_nasa.gif">
<br>
<img
id="imgWS_Standard_3"
src="_images/common/nasa_header/logo_nasa.gif">
</p>
<p>
<span id="td_WS_Standard" class="hidden">Message to
display</span>
</p>
<a href="#" onmouseover="showDesc('WS_Special',1);"
onmouseout="hideDesc('WS_Special',1);">
Link number 2</a>

<p>
<img
id="imgWS_Special_1"
src="educators/images/solarsystem/clem_full_moon_T.jpg">
<br>
<img
id="imgWS_Special_2"
src="educators/images/solarsystem/clem_full_moon_T.jpg">
<br>
<img
id="imgWS_Special_3"
src="educators/images/solarsystem/clem_full_moon_T.jpg">
</p>
<p>
<span id="td_WS_Special" class="hidden">Message to
display</span>
</p>
</body>
</html>
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fungii | last post by:
Hello, I have a stylesheet that sets p:first-letter to a certain size and colour. I was playing around with Javascript to change paragraph stylesheets using an array like this: var paras =...
2
by: windandwaves | last post by:
Hi Folk I have the following function var a = new Array(4); a = new Array(45); //status (on or off) a = new Array(45); //name of the region a = new Array(45); //on mouse over a = new...
5
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
4
by: golubovsky | last post by:
Hi, Is there an easy way to construct a function call out of a function and an array of actual arguments? e. g. given a function `fun' and an array I need to obtain equivalent of...
5
by: FrederikVds | last post by:
I have a function which can be called with an unlimited number of arguments. I want to call another function with exactly the same arguments. I know I can get the arguments in the arguments...
12
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.