I am using the following selector for an each loop in jQuery:
$("#myid").parents().andSelf().each(function({})
The selector returns an object like this:
object[html, body, div#global_container, div#content, div#myarea, span.myclass, div#myid]
That means the each loop will start with the furthest parent and then eventually ends with self
How can I reverse that order so that the each loop will start with self and work its way up the dom tree instead of down?
In order to make
reverse()work you need to change your jQuery object to anarray:http://jsfiddle.net/6VVAD/
EDIT : The
.each()function won’t work that way because we can’t chain it from a non-jQuery object as @scoota269 mentionnedI fixed it by passing the collection into the each function like so :