Monday, June 16, 2025
HomeProgrammingJavaScript closest

JavaScript closest


In the case of discovering relationships between parts, we historically consider a top-down method. We will thank CSS and querySelector/querySelectorAll for that relationship in selectors. What if we need to discover a component’s guardian primarily based on selector?

To look up the factor tree and discover a guardian by selector, you should utilize HTMLElement‘s closest technique:

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

closest seems up the ancestor chain to discover a matching guardian factor — the alternative of conventional CSS selectors. You’ll be able to present closest a easy or complicated selector to look upward for!

  • 39 Shirts – Leaving Mozilla

    In 2001 I had simply graduated from a small city highschool and headed off to a small city faculty. I discovered myself within the quaint pc lab the place the substandard computer systems featured two browsers: Web Explorer and Mozilla. It was this lab the place I fell…

  • CSS Filters

    CSS filter help lately landed inside WebKit nightlies. CSS filters present a technique for modifying the rendering of a fundamental DOM factor, picture, or video. CSS filters enable for blurring, warping, and modifying the colour depth of parts. Let’s have…

  • Animating CSS3 Transforms with MooTools Fx
  • GitHub-Style Sliding Links

    GitHub appears to alter lots however not likely change in any respect, if that makes any sense; the updates come typically however are all the time pretty small. I noticed probably the most latest updates on the pull request web page. Hyperlinks to lengthy department…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments