0

Trying to insert several images into a webpage from a JSON data provided to me. It looks like this:

"tvdb_id": 80379,
"tvrage_id": 8511,
"ended": false,
"images": {
  "poster": "http://slurm.trakt.us/images/posters/34.66.jpg",
  "fanart": "http://slurm.trakt.us/images/fanart/34.66.jpg",
  "banner": "http://slurm.trakt.us/images/banners/34.66.jpg"
},

Here is my code:

var sTvrageId = $("<p></p>").html("<b>TV RAGE ID: </b>" + data[key].tvrage_id);
var sEnded = $("<p></p>").html("<b>Ended: </b>" + data[key].ended);
var sImages = $("<img>").html((j=0, j < data[key].images.length, j++)data[key].images[j]);

However, no matter what variation I try on syntax for the loop, it won't insert the images. How can I fix this?

2
  • 1
    You should show us the code for the actual loop, not just the stuff inside it. Commented Mar 3, 2014 at 15:13
  • 1
    images.length is probably 0 because its not an array. You have to use the names of the attributes or cycle through the properties Commented Mar 3, 2014 at 15:14

1 Answer 1

4

You can't iterate inside the html() method for one image tag and expect to get three images ?

var sTvrageId = $("<p />", {html: "<b>TV RAGE ID: </b>" + data[key].tvrage_id});
var sEnded    = $("<p />", {html: "<b>Ended: </b>" + data[key].ended});
var sImages   = $([]);

$.each(data[key].images, function(_, src) {
     sImages = sImages.add(
           $('<img />', {src : src});
     );
});
Sign up to request clarification or add additional context in comments.

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.