Jquery: iterating children () in reverse order

I need to iterate over jquery children in reverse order:

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

but vice versa.

I have seen using reverse () with get ()

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

but it doesn't seem to be working with children ().

+4
source share
5 answers

Using the same strategy as before:

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

Or a little more neat:

 [].reverse.call($("#parent").children()).each(function() { # do stuff }) 
+4
source

try it

 jQuery.fn.reverse = [].reverse; $("#parent").children().reverse().each(function () { }); 

SEE HERE

+2
source

What about

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

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

+1
source

It will work

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

Demo: http://jsfiddle.net/tymeJV/maxzA/

0
source

Try this: SEE http://api.jquery.com/child-selector/

 $($("#parent > li").get().reverse()).each(function() { # do stuff }) 
0
source

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


All Articles