Web Analytics Made Easy -
StatCounter JavaScript error - undefined? - CodingForum

Announcement

Collapse
No announcement yet.

JavaScript error - undefined?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • JavaScript error - undefined?

    Hey all,
    I have come across a problem which I do not know how to solve. I have tried many things, but I can't seem to get my head around what the problem actually is.

    Here is part of the code:

    Code:
    function changeImage(imageCell) {
    
    ...
    
    var imageCellArray = new Array();
    if (q < 3) {
    	imageCellArray[q] = imageCell;
    }
    else {
    	q = 0;
    	imageCellArray[q] = imageCell;
    }
    q++;
    
    ...
    
    var imageCellArrayValue = 0;
    for (var r = 0; r < 3; r++) {
    	imageCellArrayValue = imageCellArray[r];
    	document.getElementsByName("image")[imageCellArrayValue].src = arrayOfImages[12];
    }
    Basically, the user clicks an image and as a result an imageCell value is passed to this function. That value is then stored in the array imageCellArray. The values are then retrieved, and 3 of the images are supposed to then change their src resulting in them displaying a different image.

    The error occurs here:
    document.getElementsByName("image")[imageCellArrayValue].src = arrayOfImages[12];
    When I look at Firefox's error box is says that document.getElementsByName("3x3image")[imageCellArrayValue].src = arrayOfImages[12]; is undefined.

    I don't understand the error, and I don't understand how my code is wrong . imageCellArrayValue should be a number, and that number should be able to refer to one of the images...

    Thanks for any help!
    Last edited by webguy08; Apr 3, 2009, 06:38 PM.

  • #2
    At a first glance... sounds like you're defining arrayOfImages locally (var arrayOfimages) in another function... and thus cannot access it from this function. Could be something else entirely, of course, but eh... with that much code... that's all I can say.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

    Comment


    • #3
      Would be better to see the rest of your code.
      BTW, do you actually name all your IMG tags 'image <img name="image"... ?

      Comment


      • #4
        Originally posted by Eldarrion View Post
        At a first glance... sounds like you're defining arrayOfImages locally (var arrayOfimages) in another function... and thus cannot access it from this function. Could be something else entirely, of course, but eh... with that much code... that's all I can say.
        arrayOfImages is a global array. I have defined it at the very start of the code. That array just holds URLs to images on the internet. The rest of the code in that function is very long which is why I haven't posted it, as well as the problem that it may confuse anyone who reads it because of it's untidyness.

        Originally posted by freedom_razor View Post
        Would be better to see the rest of your code.
        BTW, do you actually name all your IMG tags 'image <img name="image"... ?
        Yes, I name all of my image tags with the same name. The code I posted is the only code relevant to the problem at hand, as far as I know. Does no one have any idea what the problem might be?

        For the sake of this thread, let' just say that:
        arrayOfImages[12] = "http://codingforum.net/img/logo.gif";

        Here is an example of what the code should do:
        1. User clicks an image.
        2. The imageCell value is passed.
        3. imageCellArray holds that value.
        This is then iterated 3 times so that 3 values are held in the imageCellArray.
        4. The last 3 images are then changed to a different image by that last block of code in the original post.

        Comment


        • #5
          No one knows the problem?

          Comment


          • #6
            I am not sure whether you are storing the image data in the [imageCellArrayValue]. In that case I think its not possible.

            But you can try document.getElementsByName("image").src = arrayOfImages[12];
            Dedicated Servers - [email protected] - 1-888-869-HOST(4678)
            Award winning Managed Hosting - Dedicated Server Hosting
            Managed Dedicated Servers. Reseller Discounts. 24/7 Impressive Tech Support.

            Comment


            • #7
              An example of some of the images' code:
              Code:
              <img name="image" alt="Image" onclick="changeImage(0)">
              <img name="image" alt="Image" onclick="changeImage(1)">
              The number should be fed to the changeImage(imageCell) function.
              imageCell should then equal that number.
              imageCellArray should then store that number.

              document.getElementsByName("image").src = arrayOfImages[12];
              would work, but would not do what I want it to do, which is to change the last 3 clicked images into the value stored in arrayOfImages[12].

              If I were to create an array which held say:
              arrayOfSelectedImages[i] = document.getElementsByName("image")[i].src
              Would it be possible to manupulate that image through the array? Basically, could I do something like arrayOfSelectedImages[i] = "http://codingforum.net/img/logo.gif"; which would change the image on the page stored in the array into an image at the URL?

              Comment


              • #8
                try this:
                Code:
                	imageCellArrayValue = parseInt(imageCellArray[r]);
                best regards

                Comment


                • #9
                  I gave that a go, and the new code reads:

                  Code:
                  var imageCellArrayValue = 0;
                  for (var r = 0; r < 3; r++) {
                  	imageCellArrayValue = parseInt(imageCellArray[r]);
                  	document.getElementsByName("image")[imageCellArrayValue].src = arrayOfImages[12];
                  }
                  from

                  Code:
                  var imageCellArrayValue = 0;
                  for (var r = 0; r < 3; r++) {
                  	imageCellArrayValue = imageCellArray[r];
                  	document.getElementsByName("image")[imageCellArrayValue].src = arrayOfImages[12];
                  }
                  Which is where I assume it would go...but it didn't work

                  This problem is seriously confusing me...what the hell is wrong with Javascript

                  Comment


                  • #10
                    If you had a headache today, don't worry, it was me trying to read your mind and get that top secret code. Unfortunately, I haven't managed, so I wrote something myself.
                    Code:
                    <html>
                    <head>
                    </head>
                    <body>
                    <img src="img1.jpg" name="image" alt="Image" onclick="changeImage(0)">
                    <img src="img1.jpg" name="image" alt="Image" onclick="changeImage(1)">
                    <img src="img1.jpg" name="image" alt="Image" onclick="changeImage(2)">
                    <img src="img1.jpg" name="image" alt="Image" onclick="changeImage(3)">
                    <img src="img1.jpg" name="image" alt="Image" onclick="changeImage(4)">
                    <img src="img1.jpg" name="image" alt="Image" onclick="changeImage(5)">
                    <script>
                    var imageCellArray=[];
                    var arrayOfImages=['img2.jpg'];
                    var q=0;
                    function changeImage(imageCell) {
                    	q++; imageCellArray[q] = imageCell;
                    	if(q==3){ 
                    		for (var r=1; r<4; r++) {
                    		document.getElementsByName("image")[imageCellArray[r]].src = arrayOfImages[0];
                    		}
                    	q=0;
                    	}
                    }
                    </script>
                    </body>
                    </html>
                    That changes the source of last 3 clicked images to the URL stored in arrayOfImages, does it after clicking 3 times.

                    Comment


                    • #11
                      lol sorry about that. The code wasn't "top-secret" it was just irrelevant . I found the problem just now though! The problem was with this line:
                      var imageCellArray = new Array();
                      which I kept initialising everytime the method was called.

                      Lesson learned: don't keep initialising arrays in a method which repeatedly gets called.



                      Thanks to everyone who tried to help!
                      Last edited by webguy08; Apr 4, 2009, 01:55 PM.

                      Comment

                      Working...
                      X