Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Double arrays

  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts

    Double arrays

    Hi,
    I have an array of images:
    Code:
    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?

    Code:
    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.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could also go with an associative array:
    Code:
    <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>
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •