Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why closest() find first itself element before travesing tree

I looking for why closest() find first itself element before traversing tree.

For example: I would like fadeOut the parent div element when i click on children element, but the children element is too a div and so is the children who fadeOut

$(document).on("click", ".close", function() {
  $(this).closest("div").fadeOut();
});
.feed {
  width: 200px;
  height: 200px;
  background: red;
  position: relative;
}
.close {
  position: absolute;
  top: 0;
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="feed">
  <div class="close">
    X
  </div>
</div>

Why is itself element who fadeOut and no the parent div element?

I know parent() get the parent element, but closest() is supposed traversing elements tree.

Have you a concrete cases, where get itself is useful?

like image 548
P. Frank Avatar asked Dec 02 '15 15:12

P. Frank


People also ask

What is the use of closest?

The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent, great-grandparent, and so on. The DOM tree: This method traverse upwards from the current element, all the way up to the document's root element (<html>), to find the first ancestor of DOM elements.

What is the use of closest in Javascript?

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.

How does closest work in jquery?

The closest( selector ) method works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn't match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression.

How do I find the closest element?

To get the closest element by a selector, you can use the element's closest() method. This method starts with the target Element and traverses up through its ancestors in the DOM tree until it finds the element that matches the selector. The closest() method returns the first element that matches the selector.


2 Answers

As has been said, closest() begins with the current element. Your selector is vague enough that it stops there.

It may be safer (and more widely useful) to target a specific container class rather than the immediate ancestor div.

$(this).closest(".feed").fadeOut()
like image 25
isherwood Avatar answered Sep 21 '22 22:09

isherwood


It's because the .closest() method traverses the DOM beginning with the current element. You may want the .parents() method instead since it will begin with the parent element.

$(this).parents("div").fadeOut();

It's worth noting that the .parents() method returns zero or more elements whereas .closest() will return zero or one element. Therefore you might want to chain .first() after the .parents() method in order to get the first match:

$(this).parents("div").first().fadeOut();

$(document).on("click", ".close", function() {
  $(this).parents("div").first().fadeOut();
});
.feed {
  width: 200px;
  height: 200px;
  background: red;
  position: relative;
}
.close {
  position: absolute;
  top: 0;
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="feed">
  <div class="close">
    X
  </div>
</div>

Alternatively, you could also just exclude the parent by selecting the parent element and then using .closest() like:

$(this).parent().closest("div").fadeOut();

However, it would be much better just to select the closest .feed element rather than any div:

$(this).closest(".feed").fadeOut();

$(document).on("click", ".close", function() {
  $(this).closest(".feed").fadeOut();
});
.feed {
  width: 200px;
  height: 200px;
  background: red;
  position: relative;
}
.close {
  position: absolute;
  top: 0;
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="feed">
  <div class="close">
    X
  </div>
</div>
like image 57
Josh Crozier Avatar answered Sep 20 '22 22:09

Josh Crozier