JavaScript closest


With regards to discovering relationships between parts, 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 dad or mum based mostly on selector?

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

// 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 dad or mum component — the alternative of conventional CSS selectors. You possibly can present closest a easy or complicated selector to look upward for!

  • Create Namespaced Classes with MooTools

    MooTools has all the time gotten a little bit of grief for not inherently utilizing and standardizing namespaced-based JavaScript courses just like the Dojo Toolkit does.  Many builders create their courses as globals which is usually frowned up.  I principally disagree with that stance, however every to their very own.  In any occasion…

  • Create Spinning Rays with CSS3: Revisited
  • 9 Mind-Blowing Canvas Demos

    The <canvas> component has been a revelation for the visible consultants amongst our ranks.  Canvas gives the means for unimaginable and environment friendly animations with the added bonus of no Flash; these builders can flash their superior JavaScript expertise as an alternative.  Listed below are 9 unbelievable canvas demos that…

  • QuickBoxes for Dojo

    Including to my psychological portfolio is necessary to me. First got here MooTools, then jQuery, and now Dojo. I communicate usually with Peter Higgins of Dojo fame and determined it was time to step into his world. I selected a easy however helpful plugin…


Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles