Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active December 17, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/5593167 to your computer and use it in GitHub Desktop.
Save barneycarroll/5593167 to your computer and use it in GitHub Desktop.
$(selection).reverse(fn) applies the passed function to each element in each element in the selection recursively, starting with the bottom of the tree. Useful for cleaning out empty elements recursively (such that an empty div will be destroyed, then the parent it is only child of will be destroyed too, etc).
$.fn.reverse = function(fn){
return this.each(function(el, i){
var $all = $('<x>').append(el).find('*');
fn.call([].reverse.call($all));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment