Get count of child elements from one instance of a class
jquery, tumblr
Solution
You can cycle through each photoset item and count it's images separately and then do whatever you want with that information at that point in the code:
$(".photoset").each(function(index, elem) {
var numImages = $(this).find("img").length;
// do whatever processing you wanted to with numImages here
});
If you want to put these counts in an array, you could do it like this:
var imgCountArray = $(".photoset").map(function() {
return($(this).find("img").length)
}).get();
Problem
I'm customising a tumblr blog, and I want to apply certain styles and rules depending on the number of photos present in a photoset. What I'm not sure how to get a count of the child elements present in each post when each post has the same class. If this is the sort of code that is generated by tumblr: ``` <div class="photoset"> <img /> <img /> </div> <div class="photoset"> <img /> <img /> <img /> </div> ``` How do I get jQuery to return the number of elements in the first and second instances (i.e. 2 and 3)? I've tried using `$('.photoset img').length();` and that gives me a total count of all elements present (i.e. 5 in this case). Any help would be greatly appreciated! Cheers, Scott