Skip to content

Instantly share code, notes, and snippets.

@JTLR
Created August 8, 2014 16:17
Show Gist options
  • Save JTLR/273138044c079bd54d61 to your computer and use it in GitHub Desktop.
Save JTLR/273138044c079bd54d61 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.closest_descendent = function(filter) {
var $found = $(),
$currentSet = this.children(); // Current place
while ($currentSet.length) {
$found = $currentSet.filter(filter);
if ($found.length) break; // At least one match: break loop
// Get all children of the current set
$currentSet = $currentSet.children();
}
return $found.first(); // Return first match of the collection
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment