Skip to content

Commit e907e22

Browse files
committed
Initial commit
0 parents  commit e907e22

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

jquery.deepest.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Get the deepest children of each element in the set of matched elements, optionally filtered by a selector.
3+
*/
4+
(function ($) {
5+
$.fn.deepest = function (selector) {
6+
let deepestLevel = 0,
7+
$deepestChild,
8+
$deepestChildSet;
9+
10+
this.each(function () {
11+
$parent = $(this);
12+
$parent
13+
.find((selector || '*'))
14+
.each(function () {
15+
if (!this.firstChild || this.firstChild.nodeType !== 1) {
16+
let levelsToParent = $(this).parentsUntil($parent).length;
17+
if (levelsToParent > deepestLevel) {
18+
deepestLevel = levelsToParent;
19+
$deepestChild = $(this);
20+
} else if (levelsToParent === deepestLevel) {
21+
$deepestChild = !$deepestChild ? $(this) : $deepestChild.add(this);
22+
}
23+
}
24+
});
25+
$deepestChildSet = !$deepestChildSet ? $deepestChild : $deepestChildSet.add($deepestChild)
26+
});
27+
28+
return this.pushStack($deepestChildSet || [], 'deepest', selector || '');
29+
};
30+
}(jQuery));

jquery.deepest.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jquery.deepest.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)