Software

JavaScript closest

[ad_1]

When it comes to finding relationships between elements, we traditionally think of a top-down approach. We can thank CSS and querySelector/querySelectorAll for that relationship in selectors. What if we want to find an element’s parent based on selector?

To look up the element tree and find a parent by selector, you can use HTMLElement‘s closest method:

// Our sample element is an "a" tag that matches ul > li > a
const link = document.querySelector('li a');
const list = a.closest('ul');

closest looks up the ancestor chain to find a matching parent element — the opposite of traditional CSS selectors. You can provide closest a simple or complex selector to look upward for!

  • MooTools-Like Element Creation in jQuery

    I really dislike jQuery’s element creation syntax. It’s basically the same as typing out HTML but within a JavaScript string…ugly! Luckily Basil Goldman has created a jQuery plugin that allows you to create elements using MooTools-like syntax. Standard jQuery Element Creation Looks exactly like writing out…

  • :valid, :invalid, and :required CSS Pseudo Classes

    Let’s be honest, form validation with JavaScript can be a real bitch.  On a real basic level, however, it’s not that bad.  HTML5 has jumped in to some extent, providing a few attributes to allow us to mark fields as required or only valid if matching…


[ad_2]
Source link
Show More

admin

Makes Noise is a blog where you can find all the juicy details on a variety of topics including health and fitness, technology, lifestyle, entertainment, love and relationships, beauty and makeup, sports and so much more. The blog is updated regularly to make sure you have all the latest and greatest information on the topics that matter most to you.

Related Articles

antalya bayan escort
Back to top button