Skip to content

Instantly share code, notes, and snippets.

@ajliv
Last active August 28, 2016 06:39
Show Gist options
  • Save ajliv/a6f99c33714e720aa9a82538c1e40080 to your computer and use it in GitHub Desktop.
Save ajliv/a6f99c33714e720aa9a82538c1e40080 to your computer and use it in GitHub Desktop.
Determine if event target is a descendant of a DOM node
/**
* Determine if event target is a descendant of a DOM node
* @param {Object} event - The event object
* @param {Element} parent - The DOM node to check if event target is equal to or a descendant of
* @return {Boolean}
*/
function targetIsDescendant(event, parent) {
var node = event.target || null;
while (node !== null) {
if (node === parent) return true;
node = node.parentNode;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment