10 examples of 'classlist contains js' in JavaScript

Every line of 'classlist contains js' 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
33export function hasClass(el: Element, className: string): boolean {
34 return el.classList.contains(className);
35}
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

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}
277export function hasClass(el, classToCheck) {
278 throwIfWhitespace(classToCheck);
279
280 if (el.classList) {
281 // node.contains(otherNode)
282 return el.classList.contains(classToCheck);
283 } else {
284 return classRegExp(classToCheck).test(el.className);
285 }
286}
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}
1export function containsClassName(element: HTMLElement, classNameStr: string): boolean {
2 return String(element.className).split(' ').indexOf(classNameStr) > -1;
3}
38function hasClass(node, className) {
39 return node.className.split(' ').some(function(name) {
40 return name === className;
41 });
42}
110function hasClass(node: HTMLElement, classname: string): boolean {
111 let current;
112 const list = node.classList;
113 if (list) {
114 return list.contains(classname);
115 } else {
116 current = " " + getAttr(node, "class") + " ";
117 return current.indexOf(" " + classname + " ") > -1;
118 }
119}
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}
1export function removeFromElementClassIfContains(element, className) {
2 const classList = element.classList;
3 if (classList.contains(className)) {
4 classList.remove(className);
5 }
6}
31export function hasClass(node, className) {
32 if (node.classList) {
33 return !!className && node.classList.contains(className);
34 }
35 return node.className.split(/\s+?/).indexOf(className) > -1;
36}

Related snippets