Adrian Axinte
Adrian Axinte

Reputation: 179

Display list of images by image count variable

I have this variables:

var imagesCount = 3;
var imageUrl = "http://www.example.com/";
var imageId = "000-000-000";

and I need to get a list like this:

<img src="http://www.example.com/GetImage.aspx?cid=000-000-000&num=1" />
<img src="http://www.example.com/GetImage.aspx?cid=000-000-000&num=2" />
<img src="http://www.example.com/GetImage.aspx?cid=000-000-000&num=3" />

Any help please.

Upvotes: 0

Views: 302

Answers (4)

Oleksandr T.
Oleksandr T.

Reputation: 77482

Here implementation on JS, without jQuery.

var imagesCount = 3,
    imageUrl    = "http://www.example.com/GetImage.aspx?cid=",
    imageId     = "000-000-000",
    fragment    = document.createDocumentFragment(),
    i,
    image;

for (i = 1; i <= imagesCount; i++) {
  image = document.createElement('img');
  image.setAttribute('src', imageUrl + imageId + '&num=' + i);
  fragment.appendChild(image);
}

document.getElementById('images').appendChild(fragment)
<div id="images"></div>

Upvotes: 4

Ahmad
Ahmad

Reputation: 477

Just use this JS

    for (var i=1; i <= imagesCount;  i++){
            var img = "<img src='http: //www.example. com/GetImage.aspx?cid=000-000-000&num="+ i +" />";
            $(".yourContainerClass").append(img) ;
    }

Must have jquery for this to work

Upvotes: 3

Edi G.
Edi G.

Reputation: 2422

I'm not sure whether I get your question right, but shouldn't the following work?

for(var i=1 ; i <= imagesCount; i++){
    var img = $('<img />'); 
    img.attr('src', imageUrl + "GetImage.aspx?cid=" + imageId + "&num=" + i);
    img.appendTo('#imagediv');
}

Upvotes: 2

danmullen
danmullen

Reputation: 2510

for (i = 1; i <= imagesCount; i++) { 
    document.writeln(imageUrl + "GetImage.aspx?cid=" + imageId + "&num=" + i + " />");
}

Upvotes: 3

Related Questions