You can use this:
$($("div").get().reverse()).each(function (i) {
$(this).find("span").text(++i);
});
Another way, using also jQuery with reverse is:
$.fn.reverse = [].reverse;
$("div").reverse().each(function (i) {
$(this).find("span").text(++i);
});
This demo here. (Thank you Ian for the suggestion.)
One more alternative is to use the length
(count of elements matching that selector) and go down from there using the index
of each iteration. Then you can use this:
var nr_of_divs = $("div").length;
$("div").each(function (i) {
$(this).find("span").text(nr_of_divs - i);
});
This demo here
One more, kind of related to the one above and inspired by Adeneo's answer:
var nr_of_divs = $("div").length;
$("div span").text(function (i) {
return nr_of_divs - i;
});
Demo here