The :last selector is, in some cases, returning more than one element. Here there is a jsfiddle to try it because its hard to believe!
Code one fails:
alert($(".child").find("span:last").length); // -> alerts 3
Description: Selects the last matched element.
Note that :last selects a single element by filtering the current
jQuery collection and matching the last element within it.
Am I missing something or this is a bug?
When you use
.find()with:firstand:last, it searches for the first and last element relatively to each ancestor element that was found using$('.child').Since you have three
.childelements, you have three elements in which to search forspans. Since each.childhas exactly onespan,:lastturns up each one of those three in the context of.find(). Then.find()collects them all together, and so you have threespanelements as a result.