Skip to content

Commit

Permalink
util.js: Implement 'closest' function
Browse files Browse the repository at this point in the history
Iterate through an element's parent until it
matches the given selector.
  • Loading branch information
mrbazzan committed Sep 25, 2024
1 parent 3b92c40 commit 2476d19
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@
target.dispatchEvent( new CustomEvent(name, {bubbles: true, cancelable: false}));
};

global.closest = function closest(elem, selector){
while (elem){
if (matches(elem, selector)){
return elem;
}
elem = elem.parentElement;
}
return null;
};

})(window);

0 comments on commit 2476d19

Please sign in to comment.