Files
30-seconds-of-code/snippets/js/s/html-element-has-class.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +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