2

I created an array of images

function initialize(){
//add our 10 images to our array
imgArray[imageNum++] = new imageItem(imageDir + "img1.jpg");
imgArray[imageNum++] = new imageItem(imageDir + "img2.jpg");
imgArray[imageNum++] = new imageItem(imageDir + "img3.jpg");

}
var totalImgs = imgArray.length;

I then create a function that is linked with a in my html file:

<button id="startButton" onclick="startImage()">Start</button>

function startImage(){

    document.getElementById("pic").setAttribute("src", imgArray[0].src);
}   

My image tag : <img id="pic" src="" height="300" width="500" border="0" alt="image"> This fails to update my img object, i used firebug to look at the DOM and my array is being populated properly but the img src is not be set?

4
  • 1
    is imageNum defined? If it's not defined then imageNum++ will return NaN.. also, is imgArray defined? if it's not, then imgArray[n] will throw exception. And you probably should use imgArray.push(new imageItem(imageDir + "armory.jpg")).. Commented Feb 15, 2012 at 2:30
  • in startImage() you call imgArray[0] - but imgArray is not recognized from inside the function! Commented Feb 15, 2012 at 2:32
  • 1
    What is imageItem? Are you certain it returns an HTMLImageElement? And are you certain your imageDir is correct? Commented Feb 15, 2012 at 2:41
  • You may need to provide more details so we can help (i.e. more of your code). Otherwise, I have to ask questions like did you call initialize()? Commented Feb 15, 2012 at 3:03

3 Answers 3

3

Load multiple images sounds like a functionality that you might want to user in different projects. I would suggest to create a function that can process your images array and append the image object to a <div>:

var images = [
    'path/to/image1.png',
    'path/to/image2.png',
    'path/to/image3.png',
    'path/to/image4.png',
    'path/to/image5.png'
];

function loadImages(imgArr){
    for(var i=0; i< imgArr.length; i++) {
        console.log(imgArr[i]);
        var img = new Image();
            img.src = imgArr[i];
        document.getElementById('output').appendChild(img);
    }
}
                
loadImages(images);

You can also invoke the loadImage() function from your button:

<button onclick="loadImages(images)">Start</button>
Sign up to request clarification or add additional context in comments.

Comments

2

Your example is incomplete. You need to show what the imageItem constructor does (and it's convention to use a capital letter for the first character in a constructor), so:

function ImageItem(src) {
  this.image = new Image();
  this.src = src.
}

should do the job here. You also seem to want imgArray as a global, so declare it as one:

var imgArray;

function initialize(){
    //add our 10 images to our array 
    imgArray[imageNum++] = new imageItem(imageDir + "armory.jpg");

Assigning an array literal is a bit easier:

    imgArray = [ new ImageItem(imageDir + "armory.jpg"),
                 new ImageItem(imageDir + "eddy.jpg"),
                 ...
                 new ImageItem(imageDir + "...")
    ];
}

var totalImgs = imgArray.length;

Though I can't see why you don't just assign the array literal directly and not bother with the wrapper function. The startImage function can be a bit tidier:

function startImage(){
    document.getElementById("pic").src = imgArray[0].src;
}

Accessing element properties directly is more reliable across browsers and less to type than using setAttribute (which has quirks in some browsers).

2 Comments

The ImageItem function and the array being declared as a global helped alot. Thanks
can you show how i can assign the array without the wrapper function?
0

Modify your javascript function like this and see.

function startImage(){

    document.getElementById("pic").src =  imgArray[0].src;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.