10 examples of 'jquery check if div has class name' in JavaScript

Every line of 'jquery check if div has class name' 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

93function hasClass(el, cls) {
94 return el.className.indexOf(cls) != -1;
95}
8function hasClass(elem, className) {
9 return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
10}
198function addClassToDiv(div, className) {
199 const arr = div.className.split(" ");
200 if (arr.indexOf(className) === -1) {
201 div.className += ` ${className}`;
202 }
203}
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}
53function addClass(element, classnm) {
54 var i;
55 if (element instanceof window.NodeList) {
56 for (i = 0; i < element.length; i++) {
57 addClass(element[i], classnm);
58 }
59 return;
60 }
61 if (!isHTMLElement(element) || typeof classnm !== 'string') {
62 return false;
63 }
64 var tmp = classnm.split(' ');
65 if (tmp.length > 1) {
66 var success = true;
67 for (i = 0; i < tmp.length; i++) {
68 if (!addClass(element, tmp[i])) {
69 success = false;
70 }
71 }
72 return success;
73 }
74
75 if (hasClass(element, classnm)) {
76 return false;
77 }
78 element.className = trim([element.className, classnm].join(' '));
79 return true;
80}
888function hasClass(el, className) {
889 return getClassRegEx(className).test(el.className);
890}
31function hasClass(element, clazz) {
32 return (` ${element.className} `).indexOf(` ${clazz} `) > -1;
33}
26function hasClass (el, name) {
27 return new RegExp(' ' + name + ' ').test(' ' + el.className + ' ')
28}
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}

Related snippets