JavaScript closest

In the case 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 based mostly on selector?

To look up the factor tree and discover a father or mother 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 appears to be like up the ancestor chain to discover a matching father or mother factor — the alternative of conventional CSS selectors. You possibly can present closest a easy or complicated selector to look upward for!

Website performance monitoring
Website performance monitoring
Website performance monitoring
Website performance monitoring
  • 5 Awesome New Mozilla Technologies You’ve Never Heard Of

    5 Superior New Mozilla Applied sciences You’ve By no means Heard Of

    My journey to Mozilla Summit 2013 was unimaginable.  I’ve spent a lot time specializing in my undertaking that I had overpassed the entire nice work Mozillians have been placing out.  MozSummit supplied the right reminder of how sensible my colleagues are and the way a lot…

  • 39 Shirts – Leaving Mozilla

    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…

  • Drag. Drop. Lock.
  • “Top” Watermark Using MooTools

    “Prime” Watermark Utilizing MooTools

    Every time you will have a protracted web page value of content material, you usually wish to add a “prime” anchor hyperlink on the backside of the web page in order that your person does not need to scroll eternally to get to the highest. The one downside with this technique is…

Leave a Reply

Your email address will not be published. Required fields are marked *