10 examples of 'has class in jquery' in JavaScript

Every line of 'has class in jquery' 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
168function hasClass(elem, className) {
169 return getElem(elem).className.split(' ').has(className);
170}
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

8function hasClass(elem, className) {
9 return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
10}
93function hasClass(el, cls) {
94 return el.className.indexOf(cls) != -1;
95}
188export function hasClass(elem, className) {
189
190 // Check if there is an element and
191 // a Class Name given
192 if( !elem || !className ) return null;
193
194 // Check if it is a list of elements
195 if( elem.length > 1 ) {
196
197 // Run through the list and check if
198 // one of them has not that class Name
199 for( var i=0, max=elem.length; i<max; i++ ) {
200 if( !hasClass( elem[i], className ) ) {
201 return false;
202 }
203 }
204
205 // Not found - return false
206 return true;
207
208 } else {
209 // Check if browser supports classList
210 // Jep: Check with classList
211 // Nope: Do a regex
212 if( !!elem.classList ) {
213 return elem.classList.contains( className );
214 } else {
215 var className = " " + className + " ";
216 return ((" " + elem.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1);
217 }
218 }
219}
35function hasClass(elem, cls) {
36 return elem.classList.contains(cls)
37}
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}
33export function hasClass(el: Element, className: string): boolean {
34 return el.classList.contains(className);
35}
31function hasClass(element, clazz) {
32 return (` ${element.className} `).indexOf(` ${clazz} `) > -1;
33}
888function hasClass(el, className) {
889 return getClassRegEx(className).test(el.className);
890}
38function hasAnyClass(elm, classNames) {
39 return hasClass(elm, classNames, true);
40}

Related snippets