7 examples of 'classlist contains' in JavaScript

Every line of 'classlist contains' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
112function hasClass(element, className) {
113 if (element.classList) {
114 return element.classList.contains(className);
115 } else {
116 return (' ' + element.className + ' ').indexOf(' ' + className + ' ') >
117 -1;
118 }
119}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

33export function hasClass(el: Element, className: string): boolean {
34 return el.classList.contains(className);
35}
1export function removeFromElementClassIfContains(element, className) {
2 const classList = element.classList;
3 if (classList.contains(className)) {
4 classList.remove(className);
5 }
6}
13static includes(className: string): ?boolean {
14 if (!document.body) {
15 return null
16 }
17 return document.body.classList.contains(className)
18}
66export function hasClass(element: HTMLElement, className: string): boolean {
67 if (element.classList) {
68 return element.classList.contains(className);
69 } else {
70 return (element.className.split(" ").indexOf(className) >= 0);
71 }
72}
14function hasClass(el, className) {
15 if (el.classList) return el.classList.contains(className);
16 else
17 return !!el.className.match(new RegExp("(\\s|^)" + className + "(\\s|$)"));
18}
434hasClass (eleObj, className) {
435 return eleObj.classList.contains(className);
436}

Related snippets