Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get closest parent element (excluding current element) using a selector

I am trying to get the closest Parent element of an element.

Looking at .closest(), it seems to return the element itself if the selector matches with it:

The closest() method traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.

(emphasis is mine)

So, what would be the best way to get the closest Parent of an element given a selector?

Example

var el = document.getElementById('foo');
var closestParent = el.closest('div');
console.log(closestParent);
<div>root
  <div>level1
    <div id='foo'>level2</div>
  </div>
</div>

As you can see, el.closest('div'); returns el itself, not its closest parent matching the selector div (level1), which is what I need.

I know that in this case I can simply do closestParent.parentElement, but this is just an example, and I am trying to figure out if it possible to avoid .closest() to return the element itself.

like image 988
umbe1987 Avatar asked Jun 23 '20 09:06

umbe1987


People also ask

What is the difference between parentNode and parentElement?

parentNode gives the parent, while . parentElement gives undefined.

What does the element closest () method take as its argument and what does it return?

The closest() method searches up the DOM tree for elements which matches a specified CSS selector. The closest() method starts at the element itself, then the anchestors (parent, grandparent, ...) until a match is found. The closest() method returns null() if no match is found.

What is .closest in JavaScript?

The closest() method in JavaScript is used to retrieve the closest ancestor, or parent of the element matches the selectors. If there is no ancestor found, the method returns null.

What is closest in angular?

closest() The closest() method of the Element interface traverses the element and its parents (heading toward the document root) until it finds a node that matches the specified CSS selector.


2 Answers

The closest function is returning the correct element. It is looking for the nearest div, which happens to be itself.

If you had a structure like this:

<div>root
  <div>level1
    <span id='foo'>level2</span>
  </div>
</div>

Then you would get the level1 parent with your code.

If you want to exclude the current element, just use parentNode

var el = document.getElementById('foo');
var closestParent = el.parentNode.closest('div');
console.log(closestParent);
like image 107
AKT Avatar answered Oct 06 '22 01:10

AKT


Document of Closest Here is a DOM, it isn't about parent it's about the nearest Element so you can change your query like below but if you need parents just use parentEelement

var el = document.getElementById('foo');
var closestParent = el.closest('div:not(#foo)');
console.log(closestParent);
<div>root
  <div>level1
    <div id='foo'>level2</div>
  </div>
</div>
like image 41
H.Rafiee Avatar answered Oct 06 '22 01:10

H.Rafiee