Files
30-seconds-of-code/snippets/element-contains.md
2023-04-28 22:29:23 +03:00

641 B

title, type, tags, cover, dateModified
title type tags cover dateModified
Element contains another element snippet
browser
red-petals 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