Files
30-seconds-of-code/js/s/has-class.md
Angelos Chalaris 5c913d20bd Reorganize snippets
2023-05-03 21:19:02 +03:00

476 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Check if HTML element has class snippet javascript
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