views:

1757

answers:

1

Upon clicking on a link, I am bringing in some images from a JSON get, and then when clicking on one of the pictures can get the gallery to appear, but I would like the first image to appear straight away, as an event once the JSON has been loaded. Is this possible?

My code is:

$("#json-get")
    .css("cursor","pointer")
    .click(function(){
     $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
      function(data){
       $.each(data.items, function(i,item){
        $("<a/>").attr("rel", "group").addClass("json-im").appendTo("#imgs").attr("href", item.media.m).append(
        $("<img/>")
         .attr("src", item.media.m)
        );
       });
       $("#imgs a").fancybox();
       // Load the first element
      });
});

Many Thanks!

+1  A: 

Try adding:

  $("#imgs a:first").trigger('click');

after you create the fancybox.

tvanfosson
Brilliant. thanks. I had originally put the first link (#json-get) inside the #img div (which triggered the get again). Moving the #json-get link outside worked a treat