10 examples of 'jquery search for class' in JavaScript

Every line of 'jquery search for class' 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
17return (function getElementsByClass(searchClass,node) {
18 if (node == null) {
19 node = document;
20 }
21 var classElements = [],
22 els = node.getElementsByTagName("*"),
23 elsLen = els.length,
24 pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
25
26 for (i = 0, j = 0; i < elsLen; i++) {
27 if (pattern.test(els[i].className)) {
28 classElements[j] = els[i];
29 j++;
30 }
31 }
32 return classElements;
33})(classname, node);
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

45function JQueryClosestByClass(element, clazz) {
46 while (element) {
47 if (JQueryHasClass(element, clazz)) {
48 return element;
49 }
50 element = element.parentNode;
51 }
52 return null;
53}
31function hasClass(element, clazz) {
32 return (` ${element.className} `).indexOf(` ${clazz} `) > -1;
33}
82find (css) {
83 return this._div.querySelector(css)
84}
8function hasClass(elem, className) {
9 return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
10}
168function hasClass(elem, className) {
169 return getElem(elem).className.split(' ').has(className);
170}
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}
9function hasClass(elem, value) {
10 var className = spaced(elem.className || "");
11 return className.indexOf( " " + value + " " ) >= 0;
12}

Related snippets