andy07070 andy07070 - 2 years ago 57
Javascript Question

Trying to add images to a list

I am trying to store image sources in an array and then add them into a list using javascript.

here is my code:

var imgSrc = new Array ("images/hangings/1.jpg","images/hangings/2.jpg");
for (var i = 0; i < prices.length; i++)
$('#products ul').append('<li><img src="imgSrc[0]"/></li>');

When i load the site it displays the image boxes where i want them to be, but the images are blank. Whats wrong?/>

edit. i tried using single quotes but i got a "missing ) after argument list" error

Answer Source

imgSrc needs to be called as a function, not as a string.

Also imgSrc[0] should be imgSrc[i] so that it uses the current index your looping through (not 0 every time)

$('#products ul').append('<li><img src="' + imgSrc[i] + '"/></li>');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download