Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start .each from div id small to biggest number?

how i can make the ".each" to starts with the div id small number "1" to the big number "5" ... 1 / 2 / 3 / 4 / 5

lets say i have this 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 what im using, but starts with the first div class id number "5" but i need to start with number 1 ...

$("div[class*='TID_']").each(function() {
 // code is come here ...
});
like image 668
Mihai Viteazu Avatar asked Jul 30 '13 09:07

Mihai Viteazu


2 Answers

Try

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

Demo: Fiddle

like image 134
Arun P Johny Avatar answered Sep 20 '22 07:09

Arun P Johny


You can use index to reverse the elements.

Live Demo

elements = $("div[class*='TID_']")
elements.each(function(index) {
  current = elements.eq(elements.length - index -1);
});
like image 23
Adil Avatar answered Sep 21 '22 07:09

Adil