Last active
October 14, 2020 00:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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