Home Programming News JavaScript closest

JavaScript closest

JavaScript closest

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

To look up the component tree and discover a mother or father by selector, you need to use HTMLElement‘s closest technique:

// 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 to be like up the ancestor chain to discover a matching mother or father component — the alternative of conventional CSS selectors. You’ll be able to present closest a easy or advanced selector to look upward for!

  • fetch API

    One of many worst stored secrets and techniques about AJAX on the internet is that the underlying API for it, XMLHttpRequest, wasn’t actually made for what we have been utilizing it for.  We have accomplished effectively to create elegant APIs round XHR however we all know we are able to do higher.  Our effort to…

  • Chris Coyier’s Favorite CodePen Demos

    David requested me if I might be up for a visitor put up selecting out a few of my favourite Pens from CodePen. A frightening process! There are such a lot of! I managed to select just a few although which have blown me away over the previous few months. In the event you…

  • Chris Coyier: Some Amazing Work on CodePen III
  • Web Audio API

    The Internet Audio API permits builders to load and decode audio on demand utilizing JavaScript.  The extra I consider superior video games for Firefox OS TVs, the extra I get to find out about these APIs that I usually would not contact.  The next is a very primary introduction to the WebAudio API…



Please enter your comment!
Please enter your name here