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 […]
The post JavaScript closest appeared first on David Walsh Blog.
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!
The post JavaScript closest appeared first on David Walsh Blog.
Recommended Story For You :
![](https://clickaibank.co.in/cb-resources/img/homepage-foreground.jpg)
GET YOUR VINCHECKUP REPORT
![](https://clickaibank.co.in/cb-resources/img/645d006bb149e9165e0d11a6_MarketingBlocks-hero3-p-800.png)
The Future Of Marketing Is Here
![](https://clickaibank.co.in/cb-resources/img/charactersample.png)
Images Aren’t Good Enough For Your Audience Today!
![](https://clickaibank.co.in/cb-resources/img/wsfr_3.0_header_box.png)
Last copies left! Hurry up!
![](https://clickaibank.co.in/cb-resources/img/home_banner_image.png)
GET THIS WORLD CLASS FOREX SYSTEM WITH AMAZING 40+ RECOVERY FACTOR
![](https://clickaibank.co.in/cb-resources/img/home-feature-calendar.png)
Browse FREE CALENDARS AND PLANNERS
![](https://clickaibank.co.in/cb-resources/img/easyStep1.png)
Creates Beautiful & Amazing Graphics In MINUTES
![](https://clickaibank.co.in/cb-resources/img/bottom_bg.png)
Uninstall any Unwanted Program out of the Box
![](https://clickaibank.co.in/cb-resources/img/header_2222.jpg)
Did you know that you can try our Forex Robots for free?
![](https://clickaibank.co.in/cb-resources/img/AdBoardzCombo.png)
No responses yet