Search code examples
jqueryeachflickr

jQuery .each - run function for each instance of selector


I'm trying to create a simple Flickr plugin via the API to show a gallery of photo sets . The way I'd like it to work is something like this:

    <div id="72157634235266773" class="thumb"></div>

    <div id="72157633471728555" class="thumb"></div>

and the jQuery plugin would iterate through and find all div's with class="thumb" and plug the id value - which is the flickr photoset id - into the code to render the thumbnail and accompanying image files for the gallery.

This fiddle has my code: http://jsfiddle.net/eBGVV/3/

See how it places two of the same thumbnail images next to each other under "Photo Set 1"?

The desired result would be the correct thumb under each Photo Set 1 and 2.

I know there is a problem with how I'm using the .each but I'm not sure what?

Thanks!


Solution

  • Change the first four lines so the $(document).ready nests everything, and within the each function, you only want to refer to the current iterated instance of the .thumb using $(this)

    $(document).ready(function () {
         $('.thumb').each(function () {
              var PhotoSetID = $(this).attr("id");