Files
30-seconds-of-code/snippets/elementContains.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

639 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
elementContains browser,intermediate 2018-06-19T20:57:58+03:00 2020-11-03T22:11:18+02:00

Checks if the parent element contains the child element.

  • Check that parent is not the same element as child.
  • Use Node.contains() to check if the parent element contains the child element.
const elementContains = (parent, child) =>
  parent !== child && parent.contains(child);
elementContains(
  document.querySelector('head'),
  document.querySelector('title')
);
// true
elementContains(document.querySelector('body'), document.querySelector('body'));
// false