How to start with .each from div id from small to large numbers?

how can i get ".each" to start from a small number div id "1" to a large number "5" ... 1/2/3/4/5

lets say i have divs

<div class="TID_5">TID 5</div> <div class="TID_4">TID 4</div> <div class="TID_3">TID 3</div> <div class="TID_2">TID 2</div> <div class="TID_1">TID 1</div> 

I have this jquery that uses im but starts with the first div class identifier "5", but I need to start from number 1 ...

 $("div[class*='TID_']").each(function() { // code is come here ... }); 
+6
source share
4 answers

Try

 $("div[class*='TID_']").sort(function(e1, e2){ return $(e1).attr('class') > $(e2).attr('class') }).each(function() { console.log($(this).text()) }); 

Demo: Fiddle

+12
source

You can use the index to modify items.

Live demo

 elements = $("div[class*='TID_']") elements.each(function(index) { current = elements.eq(elements.length - index -1); }); 
+3
source
 $($("div[class*='TID_']").get().reverse()).each(function() { console.log(this); }); 

Working example http://jsfiddle.net/yBZT6/

+2
source

If your items are ordered offspring, all you have to do is the opposite. You can do this - as suggested in this answer - like this:

 $($("div[class*='TID_']").get().reverse()).each(function() { /* ... */ }); 
0
source

Source: https://habr.com/ru/post/950605/


All Articles