Mehul
Mehul

Reputation: 13

Pulling images from a JSON array

I'm having an issue pulling some images out of a json array.

here is my code

    for (var n = 0; n < detail.photos.length; n++) {
    var photos = detail.photos[n]
    var photo_img = $("<img />", {
        "src": photos
    });
    $("#Gallery").append("<br />" + photo_img)
}

what this code for my imageloop is outputting is [object, OBJECT] for each image. I tried adding an alert(photos); and what it does is give me an alertbox with the source for each image. I can't figure out how to get the images to display propery.

Ah the solution as pointed out below is that you can't use concatenation with .append()

Upvotes: 1

Views: 128

Answers (1)

Hussein Nazzal
Hussein Nazzal

Reputation: 2557

you cant use concatenation in append. cuz then you will be adding string to an object which doesnt make any sence . it should be like this

  for (var n = 0; n < detail.photos.length; n++) {

    var photos = detail.photos[n];
    console.log(photos);
    var photo_img = $("<img />", {
        "src": photos
    });
     $("body").append(photo_img).append('<br>');
}

edit : sorry i didnt notice that detail points to the hotel object

Upvotes: 0

Related Questions