Skip to content

Instantly share code, notes, and snippets.

@conectado
Last active November 16, 2017 05:45
Show Gist options
  • Save conectado/b6b794bb45b1c82022e38d56872dcf2c to your computer and use it in GitHub Desktop.
Save conectado/b6b794bb45b1c82022e38d56872dcf2c to your computer and use it in GitHub Desktop.
Getting the children of a jQuery object recursively and applying a callback.
//Adding to jQuery prototype
//Recursively applies callback to all children of a given jQuery object.
(function ($) {
$.fn.recursiveChildren = function (callback) {
$(this).children().each(
function()
{
//If it has any children
if($(this).children.length > 0)
{
$(this).recursiveChildren(callback);
}
callback($(this));
}
);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment