Skip to content

Instantly share code, notes, and snippets.

@lokimeyburg
Last active December 16, 2015 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lokimeyburg/5408951 to your computer and use it in GitHub Desktop.
Save lokimeyburg/5408951 to your computer and use it in GitHub Desktop.
$(element).closest_descendent('something') Similar to jquery .closest() but traversing decedents.
$.fn.closest_descendent = function(filter) {
var $found = $(),
$currentSet = this; // 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment