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

455 B

title, type, tags, cover, dateModified
title type tags cover dateModified
Check if HTML element has class snippet
browser
css
interior 2020-10-19T22:49:51+03:00

Checks if the given element has the specified class.

  • Use Element.classList and DOMTokenList.contains() to check if the element has the specified class.
const hasClass = (el, className) => el.classList.contains(className);
hasClass(document.querySelector('p.special'), 'special'); // true