Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created November 15, 2020 13:28
Show Gist options
  • Save AitorAlejandro/3de983eeaabf27b43c9aabab84c93205 to your computer and use it in GitHub Desktop.
Save AitorAlejandro/3de983eeaabf27b43c9aabab84c93205 to your computer and use it in GitHub Desktop.
How to check if the parent element contains the child element
const elementContains = (parent, child) => parent !== child && parent.contains(child);
// Examples
elementContains(document.querySelector('head'), document.querySelector('title'));
// true
elementContains(document.querySelector('body'), document.querySelector('body')); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment