Home Software Development JavaScript closest

JavaScript closest

JavaScript closest

In terms of discovering relationships between components, we historically consider a top-down strategy. We are able to thank CSS and querySelector/querySelectorAll for that relationship in selectors. What if we wish to discover a component’s father or mother primarily based on selector?

To look up the component tree and discover a father or mother by selector, you should utilize HTMLElement‘s closest methodology:

// Our pattern component is an "a" tag that matches ul > li > a
const hyperlink = doc.querySelector('li a');
const record = a.closest('ul');

closest appears up the ancestor chain to discover a matching father or mother component — the alternative of conventional CSS selectors. You may present closest a easy or complicated selector to look upward for!

  • CSS 3D Folding Animation
  • JavaScript Copy to Clipboard with Branding



Please enter your comment!
Please enter your name here