Skip to content

DOM

Element Contains

This snippet checks whether the parent element contains the child.

js
const elementContains = (parent, child) =>
  parent !== child && parent.contains(child)

Example

js
const head = document.querySelector('head')
const body = document.querySelector('body')

elementContains(head, body) // true

Has Class

This snippet checks whether an element has a particular class.

js
const hasClass = (el, className) => el.classList.contains(className)

Example

js
hasClass(document.querySelector('p'), 'some-class') // true

massCode released under the AGPL v3 License.
Snippet collection released under the CC-BY-4.0 License.