jquery - flickr gallery loading pictures from specified tag -


hey have flickr gallery , load pictures div 'gallery' 2 first pictures specified tag defined in 'data-category' should load div.

i have html:

    <div data-category="clouds"  class="gallery"></div>     <div data-category="mount" class="gallery"></div> 

js:

$('.gallery').each(function(index) {     var datacategory = $(this).attr('data-category');     (function() {         var flickerapi = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";         $.getjson(flickerapi, {             tags : datacategory,             tagmode : "any",             format : "json"         }).done(function(data) {             $.each(data.items, function(i, item) {                 var sourcesquare = (item.media.m).replace("_m.jpg", "_s.jpg");                 $("<img/>").attr("src", sourcesquare).appendto(".gallery");                 if (i === 1) {                     return false;                 }             });          });      })();  }); 

the problem have load first 2 pictures specified tags 'gallery' divs. supposed load 2 pictures 'gallery' specified tag given in 'data-category'

you .appendto(".gallery"), appends matching elements. if i've understood trying need append individual gallery element current outer .each() iteration.

try this:

$('.gallery').each(function(index, el) { // <-- add parameter current element     var datacategory = $(this).attr('data-category');     var flickerapi = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";     $.getjson(flickerapi, {         tags : datacategory,         tagmode : "any",         format : "json"     }).done(function(data) {         $.each(data.items, function(i, item) {             var sourcesquare = (item.media.m).replace("_m.jpg", "_s.jpg");             $("<img/>").attr("src", sourcesquare).appendto(el); // <-- use el             if (i === 1) {                 return false;             }         });     }); }); 

the $.getjson() function asynchronous, .done() function still has access el parameter containing function.

(note: i've removed immediately-invoked anonymous function since didn't seem add value.)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -