jQuery .each - run function for each instance of selector
Asked Answered
P

1

7

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!

Philibeg answered 23/6, 2013 at 6:2 Comment(0)
F
15

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");
Farinaceous answered 23/6, 2013 at 6:10 Comment(2)
Wow! That did the trick. Thank you so much. Here is the correct version: jsfiddle.net/eBGVV/4. Anyone out there that reads this please feel free to use. Just please get your own flickr api key!Philibeg
would you consider helping me with a related question? #17262935Philibeg

© 2022 - 2024 — McMap. All rights reserved.