10 examples of 'jquery closest sibling' in JavaScript

Every line of 'jquery closest sibling' 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
33function closest(nodeName, node) {
34 var reg = new RegExp('^' + nodeName + '$', 'i');
35 while (node && !reg.test(node.nodeName)) {
36 node = node.parentNode;
37 }
38 return node;
39}
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

490function closestParent(child, className) {
491 if (!child || child == document) {
492 return null;
493 }
494 if (child.classList.contains(className)) {
495 return child;
496 } else {
497 return closestParent(child.parentNode, className);
498 }
499}
22export function closest(element: Element, selector: string): Element | null {
23 return closestCallback(element, (el: Element) => matches.call(el, selector))
24}
10function closest(element, selector, top = document.body) {
11 while (!element.matches(selector)) {
12 element = element.parentNode
13 if (element === top) {
14 return null
15 }
16 }
17 return element
18}
99function findParent ($elt, className) {
100 while ($elt) {
101 if ($elt.hasClass(className))
102 return $elt;
103 $elt = $elt.parent();
104 }
105 return null;
106}
679function prevSibling(e) {
680 do {
681 e = e.previousSibling;
682 } while(e && e.nodeType != 1); //for 1st child prev will return nodeType 3. prev for that returns null
683 return e;
684}
12function closest(element, selector) {
13 var matchesFn;
14
15 // find vendor prefix
16 ['matches', 'msMatchesSelector'].some(function(fn) {
17 if (typeof document.body[fn] == 'function') {
18 matchesFn = fn;
19 return true;
20 }
21 return false;
22 })
23
24 var parent;
25
26 // Traverse parents
27 while (element) {
28 parent = element.parentElement;
29 if (parent && parent[matchesFn](selector)) {
30 return parent;
31 }
32 element = parent;
33 }
34
35 return null;
36}
96function closestTo(element, selector, target) {
97 var closestElement = closest(element, selector);
98 return contains(target, closestElement) ? closestElement : null;
99}
68export function closest (element, selector) {
69 while (element && element.nodeType === 1) {
70 if (matches(element, selector)) {
71 return element
72 }
73
74 element = element.parentNode
75 }
76
77 return null
78}
179function closest(el, selector) {
180 while (el) {
181 if (matches(el, selector)) {
182 return el;
183 } else {
184 el = el.parentElement;
185 }
186 }
187 return false;
188}

Related snippets