Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: iterate children() in reverse

Tags:

jquery

I need to iterate over jquery child elements in reverse:

$("#parent").children().each(function() {
  # do stuff
})

but in reverse.

I have seen the use of reverse() with get()

$($("li").get().reverse()).each(function() { /* ... */ });

but don't seem able to make it work with children().

like image 359
user984003 Avatar asked May 17 '13 13:05

user984003


3 Answers

Using the same strategy as before:

$($("#parent").children().get().reverse()).each(function() {
      # do stuff
})

Or slightly more neatly:

[].reverse.call($("#parent").children()).each(function() {
      # do stuff
})
like image 136
Eric Avatar answered Nov 02 '22 23:11

Eric


try this

jQuery.fn.reverse = [].reverse;

$("#parent").children().reverse().each(function () {
});

SEE HERE

like image 38
PSR Avatar answered Nov 03 '22 00:11

PSR


How about

$.each( $("#parent").children().get().reverse(), function(){
   // whatever..
} );

Demo at http://jsfiddle.net/mPsep/

like image 31
Gabriele Petrioli Avatar answered Nov 02 '22 23:11

Gabriele Petrioli