Skip to content

JavaScript closest

Relating to discovering relationships between parts, we historically bring to mind a top-down means. We will thank CSS and querySelector/querySelectorAll for that courting in selectors. What if we need to to find a component’s dad or mum in response to selector?

To seem up the part tree and discover a dad or mum via selector, you’ll be able to use HTMLElement‘s closest way:

// Our pattern part is an "a" tag that fits ul > li > a
const hyperlink = record.querySelector('li a');
const checklist = a.closest('ul');

closest appears to be like up the ancestor chain to discover a matching dad or mum part — the other of conventional CSS selectors. You’ll supply closest a easy or advanced selector to seem upward for!

  • Chris Coyier’s Favorite CodePen Demos

    David requested me if I would be up for a visitor submit selecting out a few of my favourite Pens from CodePen. A frightening activity! There are such a large amount of! I controlled to pick out a couple of regardless that that experience blown me away over the last few months. If you happen to…

  • CSS Gradients

    With CSS border-radius, I confirmed you the way CSS can bridge the space between design and building via including rounded corners to parts.  CSS gradients are every other step in that path.  Now that CSS gradients are supported in Web Explorer 8+, Firefox, Safari, and Chrome…

  • Custom Scrollbars in WebKit
  • Create Spinning, Fading Icons with CSS3 and jQuery

Ready to get a best solution for your business?