10 examples of 'jquery child with class' in JavaScript

Every line of 'jquery child with 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
45function parentWithClass(elem, classNames) {
46
47 // accept both string and array passed in
48 if (!Array.isArray(classNames)) {
49 classNames = [classNames];
50 }
51
52 while (!elem.classList || !containsAnyClass(elem.classList, classNames)) {
53 elem = elem.parentNode;
54
55 if (!elem) {
56 return null;
57 }
58 }
59
60 return elem;
61}
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

124export function addClsToFirstChild(element, className) {
125 var dom = getDom(element);
126 if (dom.firstElementChild) {
127 dom.firstElementChild.classList.add(className);
128 }
129}
6function hasParentWithClassName (el, className) {
7 while (el && el.parentNode) {
8 if (el.classList.contains(className)) {
9 return true
10 }
11 el = el.parentNode
12 }
13 return false
14}
69function findParentWithClass(element, parentClass) {
70 var parent = element.parentNode;
71 while (!$j(parent).hasClass(parentClass)) {
72 parent = parent.parentNode;
73 }
74 return parent;
75}
221export function addClass(elem, className) {
222
223 // Check if there is an element and
224 // a Class Name given
225 if( !elem || !className ) return null;
226
227 // Check if it is a list of elements
228 if( elem.length > 1 ) {
229
230 // Run through the list and add
231 // Class Name to all of them
232 for( var i=0, max=elem.length; i<max; i++ ) {
233 addClass( elem[i], className );
234 }
235
236 // Extend all Elements and return it
237 return elem;
238
239 } else {
240
241 // Check if browser supports classList
242 // Jep: Add it to classList
243 // Nope: Add it as string
244 if( !!elem.classList ) {
245 elem.classList.add( className );
246 } else if( !hasClass( elem, className ) ) {
247 elem.className += " " + className;
248 }
249
250 // return the extended Element
251 return elem;
252 }
253}
99function findParent ($elt, className) {
100 while ($elt) {
101 if ($elt.hasClass(className))
102 return $elt;
103 $elt = $elt.parent();
104 }
105 return null;
106}
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}
8function hasClass(elem, className) {
9 return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
10}
69function findParentWithClass(element, parentClass) {
70 var parent = element.parentNode;
71 while (!$(parent).hasClass(parentClass)) {
72 parent = parent.parentNode;
73 }
74 return parent;
75}
13function toggleClass(elem, className) {
14 var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
15 if(hasClass(elem, className)) {
16 while(newClass.indexOf(' ' + className + ' ') >= 0) {
17 newClass = newClass.replace(' ' + className + ' ', ' ');
18 }
19 elem.className = newClass.replace(/^\s+|\s+$/g, '');
20 } else {
21 elem.className += ' ' + className;
22 }
23}

Related snippets