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?
Modify your javascript function like this and see.
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:
You can also invode the loadImage() function from yout button:
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:should do the job here. You also seem to want
imgArray
as a global, so declare it as one:Assigning an array literal is a bit easier:
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:Accessing element properties directly is more reliable across browsers and less to type than using
setAttribute
(which has quirks in some browsers).