Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created April 8, 2020 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/bac9124b25fa49f4f5cb35a40195d236 to your computer and use it in GitHub Desktop.
Save jonathantneal/bac9124b25fa49f4f5cb35a40195d236 to your computer and use it in GitHub Desktop.
Closest, but with an extra argument to specify the ancestor to look up until
closest = (function (Object, error) {
return function (targetElement, selectors, untilElement) {
let document = Object(Object(targetElement).ownerDocument)
let rootElement = Object(document.documentElement)
let Element = Object(document.defaultView).Element
untilElement = untilElement || rootElement
if (typeof Element === 'function' && targetElement instanceof Element && untilElement instanceof Element) {
while (untilElement.contains(targetElement)) {
if (targetElement.matches(selectors)) {
return targetElement
}
targetElement = targetElement.parentElement
}
return null
} else {
throw new TypeError('Illegal invocation')
}
}
})(Object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment