js is element descendant

Solutions on MaxInterview for js is element descendant by the best coders in the world

showing results for - "js is element descendant"
Leonardo
05 Nov 2018
1const isDescendant = (child, parent) => parent.contains(child);
Allison
07 Mar 2019
1/* if you want to check deeply nested children*/
2const isDescendant = (el, parentId) => {
3  let isChild = false
4
5  if (el.id === parentId) { //is this the element itself?
6    isChild = true
7  }
8
9  while (el = el.parentNode) {
10    if (el.id == parentId) {
11      isChild = true
12    }
13  }
14  return isChild
15}