jesse1981
5/7/2013 - 11:44 PM

Find closest child element which matches given filter.

Find closest child element which matches given filter.

$.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
}