...

View Full Version : Double arrays



qwertyuiop
08-10-2006, 08:51 PM
Hi,
I have an array of images:

var images = ["image1.png", "image2.png", "image3.png"];

If I make a double array, how can I have the secondary (Img #x) values point to the (imagex.png) values?


var images = [["image1.png", "Img #1"], ["image2.png", "Img #2"], ["image3.png", "Img #3"]];

So if the user inputs "Img #1", "image1.png" will be the output. And if the user inputs "image1.png", "image1.png" is also the output.

vwphillips
08-10-2006, 09:36 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
var images = [["image1.png", "Img #1"], ["image2.png", "Img #2"], ["image3.png", "Img #3"]];

function Tst(){
var args=Tst.arguments;
for (var zxc0=0;zxc0<args[0].length;zxc0++){
for (var zxc1=0;zxc1<args[0][zxc0].length;zxc1++){
for (var zxc2=1;zxc2<args.length;zxc2++){
if (args[zxc2]==args[0][zxc0][zxc1]){
alert(args[0][zxc1][0]);
return args[0][zxc1][0];
}
}
}
}
}


//-->
</script>
</head>

<body onload="Tst(images,'Img #1','image1.png'); ">
</body>

</html>

Pyth007
08-11-2006, 02:05 PM
You could also go with an associative array:


<html><head>
<script type="text/javascript">
numberImages = 5;
myImages=new Object();

for (var index=1; index<=numberImages; index++)
{
myImages['Img'+index]='image'+index+'.png';
}

function showImage()
{
var imgNumber = Number(document.getElementById('inputBox').value);
if (isNaN(imgNumber) || (imgNumber>numberImages) || (imgNumber<1))
{
alert('Please enter a number 1 through '+numberImages);
setTimeout(document.getElementById('inputBox').focus, 100);
return false;
}
document.getElementById('myPic').src=myImages['Img'+imgNumber];
}
</script></head><body>
<img style="width: 500px; height: 250px; border: 0;" src="default.png" id="myPic" />
<label for="inputBox">Enter in a number: </label>
<input type="text" id="inputBox" name="inputBox" />
<input type="button" value="Get Image!" onclick="showImage();" />
</body></html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum