Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Latest commit

 

History

History
39 lines (27 loc) · 715 Bytes

File metadata and controls

39 lines (27 loc) · 715 Bytes
title JavaScript DOM
titleTemplate Snippet Collection | massCode
description Collection of JavaScript DOM snippets

DOM

Element Contains

This snippet checks whether the parent element contains the child.

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

Example

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

elementContains(head, body) // false

Has Class

This snippet checks whether an element has a particular class.

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

Example

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