Skip to content

Instantly share code, notes, and snippets.

@adelin-b
Last active October 14, 2020 00:12
Show Gist options
  • Save adelin-b/43c9b8f0828d6965b2aab01a2ee04210 to your computer and use it in GitHub Desktop.
Save adelin-b/43c9b8f0828d6965b2aab01a2ee04210 to your computer and use it in GitHub Desktop.
Check if a dom element is the descendant from another element with matching id
/** Check if a dom element is the descendant from another element with matching id */
export const isDescendant = (element: HTMLElement, parentId: string, depth: number) => {
let isChild = false
if (element.id === parentId) {
//is this the element itself?
isChild = true
}
let iterations = 0
while ((element = element.parentNode as HTMLElement) && iterations++ < depth) {
if (element.id == parentId) {
isChild = true
}
}
return isChild
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment