Jquery .Each() Backwards

JQuery .each() backwards

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

Reverse object in jQuery.each

As it is, you can't in any reliable manner. Because you are enumerating an Object, there is never a guaranteed order.

If you want a guaranteed numeric order, you would need to use an Array, and iterate backwards.


EDIT: This will convert your Object to an Array, and do a reverse iteration.

Note that it will only work if all the properties are numeric.

var data = $.parseJSON($('#sdata').val());
var arr = [];

for( var name in data ) {
arr[name] = data[name];
}
var len = arr.length;
while( len-- ) {
if( arr[len] !== undefined ) {
console.log(len,arr[len]);
}
}

how to iterate elements in reverse order in jquery?

try this

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

jquery: reverse an order

If you have a container around the list, it's a little easier:

$("#container").append($(".block-item").get().reverse());

http://jsfiddle.net/BhTEN/12/

JQuery reverse each() with find()

You excluded the call to the get()[docs] method.

   // --------------------v
$($(xml).find('concert').get().reverse()).each(function() {

This is needed to get an Array of the elements from the jQuery object. This is what allows you to call .reverse(), which is on Array.prototype.

jquery: iterate children() in reverse

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
})

Is there an inbuilt way to use JQuery's .each() in reverse?

Use this

$($("#nav a").get().reverse()).each(function() { 
//..........
});


Related Topics



Leave a reply



Submit