Files
30-seconds-of-code/snippets/elementContains.md
Isabelle Viktoria Maciohsek 05e3f6dc07 Format snippets
2020-11-03 22:11:18 +02:00

563 B

title, tags
title tags
elementContains browser,intermediate

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