10 examples of 'jquery closest child' in JavaScript

Every line of 'jquery closest child' 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
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}
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

22export function closest(element: Element, selector: string): Element | null {
23 return closestCallback(element, (el: Element) => matches.call(el, selector))
24}
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}
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}
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}
260function closest(element, selector) {
261 if (element.closest) {
262 return element.closest(selector);
263 }
264 var el = element;
265 while (el) {
266 if (matches(el, selector)) {
267 return el;
268 }
269 el = el.parentElement;
270 }
271 return null;
272}
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}
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}
71export function closest(elem, selector) {
72 if (elem.closest) {
73 return elem.closest(selector);
74 }
75 while (elem) {
76 if (matches(elem, selector)) {
77 return elem;
78 }
79 elem = elem.parentElement;
80 }
81 return null;
82}

Related snippets