473,385 Members | 2,013 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,385 software developers and data experts.

variable as array index

I want to do something like:
var img_index="dog";
document.images[img_index] = "blah";

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.
Feb 25 '07 #1
11 7560
On Feb 24, 11:01 pm, Jammer <ask...@mail.comwrote:
I want to do something like:
var img_index="dog";
document.images[img_index] = "blah";

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.
Using FF I was unable to duplicate your issue but you may want to try
forcing an expression evaluation by enclosing the index in ()'s; ie:

document.images[(img_index)]

- Craig Taylor
http://www.ctalkobt.net

Feb 25 '07 #2
Craig Taylor said the following on 2/24/2007 11:34 PM:
On Feb 24, 11:01 pm, Jammer <ask...@mail.comwrote:
>I want to do something like:
var img_index="dog";
document.images[img_index] = "blah";

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.

Using FF I was unable to duplicate your issue but you may want to try
forcing an expression evaluation by enclosing the index in ()'s; ie:

document.images[(img_index)]
What exactly do you think that accomplishes by wrapping it in ()?
Part of the issue is going to be trying to set
document.images[var]="blah" as document.images[var] is going to be
referencing an object and then you are trying to set that object equal
to something else. Setting a property of the image is a different
scenario though.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 25 '07 #3
Jammer wrote:
I want to do something like:
var img_index="dog";
c

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.
I want to:

document.images[img_index] = "blah";
where img_index = "dog";
so effectively. I want:
document.images["dog"] = "blah";

but then I want:
img_index="cat";
document.images[img_index] = "blah2";

Here is the code I tried:

function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;
}

I want to call:
openNav( "dog" );
openNav( "cat" );
Feb 25 '07 #4
Lee
Jammer said:
>
Jammer wrote:
>I want to do something like:
var img_index="dog";
c

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.

I want to:

document.images[img_index] = "blah";
where img_index = "dog";
so effectively. I want:
document.images["dog"] = "blah";

but then I want:
img_index="cat";
document.images[img_index] = "blah2";

Here is the code I tried:

function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;
The document.images[] array can only be indexed by numbers.
document.images["dog"] simply assigns a new attribute to the
images object (in browsers where it's legal to do that).

You can do:

img_index=2;
document.images[img_index].src=img_path;
if there are at least three images in the page.
--

Feb 25 '07 #5
Lee said the following on 2/25/2007 12:21 AM:
Jammer said:
>Jammer wrote:
>>I want to do something like:
var img_index="dog";
c

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.
I want to:

document.images[img_index] = "blah";
where img_index = "dog";
so effectively. I want:
document.images["dog"] = "blah";

but then I want:
img_index="cat";
document.images[img_index] = "blah2";

Here is the code I tried:

function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;

The document.images[] array can only be indexed by numbers.
Say again? I have never seen anywhere where the document.images array is
numeric index only. In fact, document.images['imageNAME'] is the most
cross-browser way to access an image object.
document.images["dog"] simply assigns a new attribute to the
images object (in browsers where it's legal to do that).
In what browser does this fail:

<img name="image1" src="myImage1.jpg">
<img name="image2" src="myImage2.jpg">
<img name="image3" src="myImage3.jpg">
<select onchange="alert(document.images[this.value].src)">
<option value="image1">image1
<option value="image2">image2
<option value="image3">image3
</select>

You can do the reverse as well:

<img name="image1" src="myImage1.jpg">
<img name="image2" src="myImage2.jpg">
<img name="image3" src="myImage3.jpg">
<select onchange="document.images[this.value].src='someThingElse')">
<option value="image1">image1
<option value="image2">image2
<option value="image3">image3
</select>
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 25 '07 #6
"Randy Webb" <Hi************@aol.comwrote in message
news:cc********************@telcove.net...
Lee said the following on 2/25/2007 12:21 AM:
<select onchange="document.images[this.value].src='someThingElse')">
Minus that last parenthesis, from your alert example. : )

-Lost
Feb 25 '07 #7
Lee
Randy Webb said:
>The document.images[] array can only be indexed by numbers.

Say again? I have never seen anywhere where the document.images array is
numeric index only. In fact, document.images['imageNAME'] is the most
cross-browser way to access an image object.
I had read the request as to create meaningful entries in the array with
arbitrary string values, and worded my response badly.
--

Feb 25 '07 #8
-Lost said the following on 2/25/2007 1:50 AM:
"Randy Webb" <Hi************@aol.comwrote in message
news:cc********************@telcove.net...
>Lee said the following on 2/25/2007 12:21 AM:
><select onchange="document.images[this.value].src='someThingElse')">

Minus that last parenthesis, from your alert example. : )
Yes indeed :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 25 '07 #9
Randy Webb wrote:
>>function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;
<img name="image3" src="myImage3.jpg">
<select onchange="alert(document.images[this.value].src)">
Wouldn't it make more sense to be this.name instead of this.value?
I don't what I did but it works now. :-)
function openNav( button_name ) {
// alert( "button_name='" + button_name + "'" );
var img_path = "/images/nav/" + button_name + "_open.png";
document.images[button_name].src = img_path;
}

<a href="\" onMouseOver="openNav('home');"><img
src="/images/nav/home.png" alt="home button" name="home" border=0></a>

I would like to use the name of the image inside the href instead of
hardcoding 'home'.
I tried giving a name='home' to the href and using openNav(this.value)
but that was undefined.
Feb 25 '07 #10
"Jammer" <as****@mail.comwrote in message
news:bd**************************@NEXICOM.NET...
Randy Webb wrote:
>>>function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;
><img name="image3" src="myImage3.jpg">
<select onchange="alert(document.images[this.value].src)">

Wouldn't it make more sense to be this.name instead of this.value?
No, because it is referencing the OPTION's VALUE attribute (not the IMG's NAME attribute).

-Lost
Feb 26 '07 #11
Jammer said the following on 2/25/2007 4:47 PM:
Randy Webb wrote:
>>>function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;
><img name="image3" src="myImage3.jpg">
<select onchange="alert(document.images[this.value].src)">

Wouldn't it make more sense to be this.name instead of this.value?
No. In the onchange of the select this.value refers to the value of the
selected option. If you change it to this.name then this.name refers to
the name attribute of the select itself - which it doesn't have.
>
I don't what I did but it works now. :-)
function openNav( button_name ) {
// alert( "button_name='" + button_name + "'" );
var img_path = "/images/nav/" + button_name + "_open.png";
document.images[button_name].src = img_path;
}

<a href="\" onMouseOver="openNav('home');"><img
src="/images/nav/home.png" alt="home button" name="home" border=0></a>

I would like to use the name of the image inside the href instead of
hardcoding 'home'.
I tried giving a name='home' to the href and using openNav(this.value)
but that was undefined.
<a href="\">
<img src="/images/nav/home.png"
onmouseover="openNav(this.name)"
alt="home button"
name="home"
<border="0">
</a>

function openNav(imageName){
currentSource=document.images[imageName].src
newSource=currentSource.substring(0,currentSource. length-4)+"_open.png"
document.images[imageName].src=newSource
}
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 26 '07 #12

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

Similar topics

4
by: Gord | last post by:
Hello, I think that what I'm trying to do is impossible, but before I give up I thought I'd try and pick a few more knowledgeable brains than my own. I have any array of user defined type...
6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
11
by: - Steve - | last post by:
For a school assignment I need to write a class to work with the following code. The IntArray b(-3, 6) basically means that I need to produce an array of integer values that has an index going...
9
by: mps | last post by:
Suppose..... arr = Array("only","a","test"); ArrayVar = "arr"; alert(ArrayVar); This the simplyfied version of my problem.. (alerting 'undefined' instead of 'only'...)
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
7
by: icosahedron | last post by:
Is there a way to determine if a parameter to a function is a constant (e.g. 2.0f) versus a variable? Is there some way to determine if this is the case? (Say some metaprogramming tip or type...
1
by: Vinod | last post by:
Hi, In VC8 project, I am having a struct which is having a char* variable. Now I am creating a 3 elements array object for the struct. I send the base address of the object using VARIANT to a...
6
by: Vyoma | last post by:
This is quite a bit of problem I am facing, and I cannot point exactly where I am going wrong. I have been lurking around at several forums with regard to login and user authentication scripts and...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
1
by: BiraRai | last post by:
is it possible to add new variable to a array object at run time? the following code does not work. $iterator->current()-> = 1234; how can this be done? function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.