JavaScript closest


On the subject of discovering relationships between components, 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 guardian based mostly on selector?

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

// Our pattern aspect 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 guardian aspect — the other of conventional CSS selectors. You may present closest a easy or advanced selector to look upward for!

  • Designing for Simplicity

    Earlier than we get began, it is price me spending a short second introducing myself to you. My title is Mark (or @integralist if Twitter occurs to be your communication software of alternative) and I presently work for BBC Information in London England as a principal engineer/tech…

  • Vibration API

    Most of the new APIs supplied to us by browser distributors are extra focused towards the cell consumer than the desktop consumer.  A type of easy APIs the Vibration API.  The Vibration API permits builders to direct the machine, utilizing JavaScript, to vibrate in…


Please enter your comment!
Please enter your name here