user3231419
user3231419

Reputation: 295

Display json images with jQuery

I'm trying to display JSON array-data using jQuery. I'm using other examples that I found on internet but I keep getting errors and problems.

When I try confirm(val.image); I get a pop-up: "unidentified".

jQuery:

$(document).ready(function(){

    $.getJSON( "images.json", function( data ) {
        $.each( data, function( key, val ) {
                confirm(val.image);
        });    
    });

});

images.json

{
    "images": [
        {
             "name": "Image 1",
             "image": "images/image1.jpg"
        },
        {
             "name": "Image 2",
             "image": "images/image2.jpg"
        }
    ]
}

(I'm using wamp/localhost).

Upvotes: 0

Views: 562

Answers (2)

elad.chen
elad.chen

Reputation: 2425

Assuming you are able to get the JSON properly

You should use data.images instead of data (data is an object where data.images is the array to loop through)

$.each(data.images, function(index, value) {
    alert(value.image);
});

http://jsfiddle.net/361wr7Lg/

Upvotes: 1

PeterKA
PeterKA

Reputation: 24648

Try this:

    $.each( data.images, function( i, val ) {
            alert(val.image);
            alert(val.name);
    });

Upvotes: 1

Related Questions