js is element descendant code example
Example 1: javascript Check if an element is a descendant of another
const isDescendant = (child, parent) => parent.contains(child);
Example 2: js is element descendant
/* if you want to check deeply nested children*/
const isDescendant = (el, parentId) => {
let isChild = false
if (el.id === parentId) { //is this the element itself?
isChild = true
}
while (el = el.parentNode) {
if (el.id == parentId) {
isChild = true
}
}
return isChild
}