Dave
Dave

Reputation: 90

how to make a specific loop on $.getJSON and $.each jquery function?

I have this code to retrieve Flickr images. However, I just want to retrieve only let say 4 images, how to edit this code?

Thanks!

jQuery(document).ready(function($){
                $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=<?php print $flickrid; ?>&lang=en-us&format=json&jsoncallback=?", function(data){
                      $.each(data.items, function(index, item){
                            $("<img/>").attr("src", item.media.m).appendTo("#flickr")
                              .wrap("<li><a href='" + item.link + "'></a></li>");
                      });
                    });
            });

Upvotes: 0

Views: 1589

Answers (2)

Ralph Lavelle
Ralph Lavelle

Reputation: 5769

The accepted answer doesn't limit the amount of images you're retrieving from Flickr. It merely ignores some of the results that are returned. But if that's what you meant, and you accepted the answer so I'm guessing it was, then here's something even lazier:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=<?php print $flickrid; ?>&lang=en-us&format=json&jsoncallback=?", function(data){
    $.each(data.items, function(index, item){
        $("<img/>:lt(5)").attr("src", item.media.m).appendTo("#flickr")
                         .wrap("<li><a href='" + item.link + "'></a></li>");
        }
    });
});

Note the ":lt(5)" filter.

Upvotes: 1

adeneo
adeneo

Reputation: 318342

The lazy way:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=<?php print $flickrid; ?>&lang=en-us&format=json&jsoncallback=?", function(data){
    $.each(data.items, function(index, item){
        if (index<5) {
            $("<img/>").attr("src", item.media.m).appendTo("#flickr")
                       .wrap("<li><a href='" + item.link + "'></a></li>");
            });
        }
    });
});

Upvotes: 1

Related Questions