kiinlam
4/19/2016 - 8:29 AM

Check if the parent node contains the given DOM node. Returns false if both are the same node

Check if the parent node contains the given DOM node. Returns false if both are the same node

var contains = document.documentElement.contains ?
  function(parent, node) {
    return parent !== node && parent.contains(node)
  } :
  function(parent, node) {
    while (node && (node = node.parentNode))
      if (node === parent) return true
    return false
  }