Answers for "js is element descendant"

14

javascript Check if an element is a descendant of another

const isDescendant = (child, parent) => parent.contains(child);
Posted by: Guest on July-03-2020
1

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
}
Posted by: Guest on January-20-2021

Code answers related to "js is element descendant"

Code answers related to "Javascript"

Browse Popular Code Answers by Language