17.4 C
London
Tuesday, September 3, 2024

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 need to discover a component’s mum or dad based mostly on selector?

To look up the ingredient tree and discover a mum or dad by selector, you need to use HTMLElement‘s closest technique:

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

closest seems up the ancestor chain to discover a matching mum or dad ingredient — the alternative of conventional CSS selectors. You may present closest a easy or complicated selector to look upward for!

  • 5 HTML5 APIs You Didn’t Know Existed

    If you say or learn “HTML5”, you half count on unique dancers and unicorns to stroll into the room to the tune of “I am Attractive and I Know It.”  Are you able to blame us although?  We watched the basic APIs stagnate for therefore lengthy {that a} primary characteristic…

  • 9 Mind-Blowing WebGL Demos

    As a lot as builders now detest Flash, we’re nonetheless enjoying a little bit of catch as much as natively duplicate the animation capabilities that Adobe’s outdated expertise offered us.  In fact we’ve got canvas, an superior expertise, one which I highlighted 9 mind-blowing demos.  One other expertise out there…

  • RealTime Stock Quotes with MooTools Request.Stocks and YQL

    It goes with out saying however MooTools’ inheritance sample permits for creation of small, easy lessons that possess immense energy.  One instance of that energy is a category that inherits from Request, Request.JSON, and Request.JSONP:  Request.Shares.  Created by Enrique Erne, this nice MooTools class acts as…

  • Jack Rugile’s Favorite CodePen Demos

    CodePen is an incredible supply of inspiration for code and design. I’m blown away on daily basis by the demos customers create. As you may see under, I’ve an affinity towards issues that transfer. It was tough to slender down my favorites, however right here they’re!


Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here